xunin

Linux server start/stop/restart & global message script

11 posts in this topic

hey, i have setup my exile mod server and wanted to ask if someone can share a stat/stop/restart and global message(every x minutes spam) script for linux

all tools that i´ve found are designed for windows

Share this post


Link to post
Share on other sites
Advertisement

but found this too ..

Also, if youre trying to run Bec under Linux/Wine you MUST use the --dsc startup option.

Edited by TheJoker

Share this post


Link to post
Share on other sites

Based on the code I found here:

https://raw.githubusercontent.com/katzsmile/DayZAdmin/master/modules/rcon.php

Create a file config.php

<?php

	$serverip = "127.0.0.1";
	$serverport = "2302";
	$rconpassword = "xxxx";
?>

create rcon.php

<?php 

function strToHex($string)
{
    $hex='';
    for ($i=0; $i < strlen($string); $i++)
    {
        $hex .= dechex(ord($string[$i]));
    }
    return $hex;
}

function hexToStr($hex)
{
    $string='';
    for ($i=0; $i < strlen($hex)-1; $i+=2)
    {
        $string .= chr(hexdec($hex[$i].$hex[$i+1]));
    }
    return $string;
}

function computeUnsignedCRC32($str){
   sscanf(crc32($str), "%u", $var);
   $var = dechex($var + 0);
   return $var;
}
 
function dec_to_hex($dec)
{
    $sign = ""; // suppress errors
	$h = null;
    if( $dec < 0){ $sign = "-"; $dec = abs($dec); }

    $hex = Array( 0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5,
                  6 => 6, 7 => 7, 8 => 8, 9 => 9, 10 => 'a',
                  11 => 'b', 12 => 'c', 13 => 'd', 14 => 'e',   
                  15 => 'f' );
       
    do
    {
        $h = $hex[($dec%16)] . $h;
        $dec /= 16;
    }
    while( $dec >= 1 );
   
    return $sign . $h;
} 

function get_checksum($cs)
{
    $var = computeUnsignedCRC32($cs);
	//echo "crchex: ".$var."<br/>";
	$x = ('0x');
	$a = substr($var, 0, 2);
	$a = $x.$a;
	$b = substr($var, 2, 2);
	$b = $x.$b;
	$c = substr($var, 4, 2);
	$c = $x.$c;
	$d = substr($var, 6, 2);
	$d = $x.$d;
	return chr($d).chr($c).chr($b).chr($a);
} 

function rcon($serverip,$serverport,$rconpassword,$cmd){
	$passhead = chr(0xFF).chr(0x00);
	$head = chr(0x42).chr(0x45);
	$pass = $passhead.$rconpassword;
	$answer = "";
	$checksum = get_checksum($pass);

	$loginmsg = $head.$checksum.$pass;

	$rcon = fsockopen("udp://".$serverip, $serverport, $errno, $errstr, 1);
	stream_set_timeout($rcon, 1);

	if (!$rcon) {
		echo "ERROR: $errno - $errstr<br />\n";
	} else {
		fwrite($rcon, $loginmsg);
		$res = fread($rcon, 16);
		
		$cmdhead = chr(0xFF).chr(0x01).chr(0x00);
		//$cmd = "Players";
		$cmd = $cmdhead.$cmd;
		$checksum = get_checksum($cmd);
		$cmdmsg = $head.$checksum.$cmd;
		$hlen = strlen($head.$checksum.chr(0xFF).chr(0x01));
		
		fwrite($rcon, $cmdmsg);
		$answer = fread($rcon, 102400);
		
		if ( strToHex(substr($answer, 9, 1)) == "0"){
			$count = strToHex(substr($answer, 10, 1));
			//echo $count."<br/>";
			for ($i = 0; $i < $count-1; $i++){
				$answer .= fread($rcon, 102400);
			}
		}
		//echo strToHex(substr($answer, 0, 16))."<br/>";
		//echo strToHex($answer)."<br/>";
		//echo $answer."<br/>";
		$cmd = "Exit";
		$cmd = $cmdhead.$cmd;
		$checksum = get_checksum($cmd);
		$cmdmsg = $head.$checksum.$cmd;
		fwrite($rcon, $cmdmsg);
	}

	return $answer;
}


?>

create warning.php

<?php

	include("rcon.php");
	include("config.php");
	
	$msg = $argv[1];
	$cmd = "say -1 $msg";
	rcon($serverip,$serverport,$rconpassword,$cmd);

?>

You can call these from a crontab:

30 1,3,5,7,9,11,13,15,17,19,21,23 * * * php -q /home/exile/arma3/restarter/warning.php "RESTART WARNING Server restarting in 30 minutes"
50 1,3,5,7,9,11,13,15,17,19,21,23 * * * php -q /home/exile/arma3/restarter/warning.php "RESTART WARNING Server restarting in 10 minutes"
55 1,3,5,7,9,11,13,15,17,19,21,23 * * * php -q /home/exile/arma3/restarter/warning.php "RESTART WARNING Server restarting in 5 minutes"
58 1,3,5,7,9,11,13,15,17,19,21,23 * * * php -q /home/exile/arma3/restarter/warning.php "RESTART WARNING Server restarting in 2 minutes"
59 1,3,5,7,9,11,13,15,17,19,21,23 * * * php -q /home/exile/arma3/restarter/warning.php "RESTART WARNING Server restarting in 1 minute"
0  0,2,4,6,8,10,12,14,16,18,20,22 * * * ./home/exile/exile_linux_startscript.sh restart

 

Edited by second_coming
3 people like this

Share this post


Link to post
Share on other sites

@second_coming  could you give me a short explanation on how to create those specific crontabs? I have never worked with cron before

Share this post


Link to post
Share on other sites

Essentially the example above is for a 2 hour restart with warnings at 30,10,5,2,1 minutes then on the deadline the restart script is triggered

have a read of this https://en.wikipedia.org/wiki/Cron to explain how to format them correctly.

Once you get your head around crontabs you will wish it was as simple with Windows :)

 

 

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.