Emergency Reboot Script

Post here your questions about SFS2X. Here we discuss all server-side matters. For client API questions see the dedicated forums.

Moderators: Lapo, Bax

andrew2110
Posts: 78
Joined: 28 May 2008, 13:36

Emergency Reboot Script

Postby andrew2110 » 02 Feb 2018, 08:57

Over the past couple of months, our server has gotten bogged down with varying and some slightly suspicous activity being shown in the logs. This normally happens when I've gone to sleep and don't find out about it till I wake up to a big mess in the morning. To try and combat this, I've tried to write a script that when enough of my moderators have reported a reboot is required, the script will run and try to reboot the server. It finds the process ID, kills it, and those two bits work fine, however I can't get it to automatically restart the server!

The specific line of code that I can't figure out how to make it work is:

exec('/home/SFS2X/sfs2x-service start',$output2);

Has anyone got experience of PHP exec commands / cron jobs. This is run as root as a cron job every minute.

Code: Select all

<?

date_default_timezone_set('Europe/London');
include("globals.php");

$query = mysqli_query($conn,'SELECT * FROM emergency');
if(mysqli_num_rows($query)>0) {
   $delete = mysqli_query($conn,'DELETE FROM emergency WHERE 1');
exec('ps aux', $output);

// Go through all the output from ps aux to find one that is owned by  root and is java
for($i=0;$i<sizeof($output);$i++)
{
   if(strpos($output[$i],'/usr/bin/java')>0) {
   $findNextNonNull = false;
   $processID = 0;
      $details = explode(' ',$output[$i]);
      for($i2=0;$i2<sizeof($details);$i2++) {
         if($details[$i2]=='root') {
            $findNextNonNull = true;
         } else {
            if($findNextNonNull) {
               if(strlen($details[$i2])>0) {
                  $processID = $details[$i2];
                  echo $processID;
                  // Kill the process, this bit works
                  exec('kill -9 '. $processID.'',$output1);
                  echo '<h1>Output from sig kill</h1>';
                  var_dump($output1);
                  sleep(5);
                  echo '<h1>Rebooting Server</h1>';
                  
                  // This bit doesn't work...
                  exec('/home/SFS2X/sfs2x-service start',$output2);
                  var_dump($output2);
                  exit;
               }
            }
         }
      }
   }
   
   
}
}
?>
User avatar
Lapo
Site Admin
Posts: 20392
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Emergency Reboot Script

Postby Lapo » 02 Feb 2018, 09:04

Hi,
what is exactly the error when the PHP script attempts to start the server?
You should get some clues from the $output2 variable, right?

Thanks
Lapo
--
gotoAndPlay()
...addicted to flash games
andrew2110
Posts: 78
Joined: 28 May 2008, 13:36

Re: Emergency Reboot Script

Postby andrew2110 » 02 Feb 2018, 09:19

I've set it up to send e-mails to me now and have changed it so that its running sfs2x-service status instead:

testing JVM in /home/jre ...
testing JVM in /usr ...
The daemon is running.

Is what its sending me every minute at the moment. Thanks for checking it over, was just wondering if there was any obvious errors but as the output from the script above looks like I think it should. I don't dare keep on booting everyone out of the game to test the script, I'll update the server later and do a reboot using the emergency script and see if it gives me any reasonable info as to whats happening.
User avatar
Lapo
Site Admin
Posts: 20392
Joined: 21 Mar 2005, 09:50
Location: Italy

Re: Emergency Reboot Script

Postby Lapo » 02 Feb 2018, 09:51

Can't you test the script on a local machine? That way you can make sure it works before deploying it live.

Cheers
Lapo

--

gotoAndPlay()

...addicted to flash games

Return to “SFS2X Questions”

Who is online

Users browsing this forum: No registered users and 16 guests