Restart SA:MP hosted server trough a php code
#8

to restart the server you need to kill the process then start it again;

I took this snippet from my own server administration script, it is not a finished script, it is just to look and see how it works, then modify it to work with your server;

PHP код:
$cmd ssh2_exec($connection"top -b -n 1 | grep samp");
stream_set_blocking($cmdtrue);
$resp stream_get_contents($cmd);
preg_match_all("/[ ]+(.*?) (.*?) [ ]+(.*?) [ ]+(.*?)[ ]+(.*?)[ ]+(.*?)[ ]+(.*?) (.*?) [ ]+(.*?) [ ]+(.*?) [ ]+(\d+\:\d+\.\d+) (.*?) .*?\n/i"$resp$resp);
$erros = array();
for(
$i 0$i count($resp[1]); ++$i) {
    
$cmd ssh2_exec($connection"kill {$resp[1][$i]}");
    
stream_set_blocking($cmdtrue);
    
    
$erro stream_get_contents(ssh2_fetch_stream($cmdSSH2_STREAM_STDERR));
    if(
stristr($erro"No such process")) {
        
$erros[] = "Could not kill the process '{$resp[12][$i]}' (pid: {$resp[1][$i]})";
    }
}
if(
count($erros) > 0) {
    die(
json_encode(array("resp"=>utf8_encode("There was a error while restarting the server\\n\\n".implode("\\n"$erros)))));
} else {
    die(
json_encode(array("resp"=>utf8_encode("Server restarted successfully."))));

In my case, my server have an auto-restarter, so I just have to kill the process and the auto-restarted will put it back online again automatically.


Also you need to have sure if SSH2 library is installed, just make a test;

PHP код:
if(function_exists("ssh2_connect")) die("SSH2 library installed!");
else die(
"SSH2 library not installed!"); 
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)