Blog

PHP Reboot Request System


PHP Reboot Request System: Restarting Server Using PHP and Cron Tab Sometimes there are errors on our server that could have been easily fixed by a restart, but since we're sleeping, away or whatever, it causes some damage and even catastrophe maybe. So I constructed an idea of PHP Reboot Request System by using a temporary file, using it's existence to signal the root user to reboot the server. I imagine this could be done with a root crontab line that looks like this:
* * * * * [ -f /tmp/php_request-reboot ] && unlink /tmp/php_request-reboot & & sudo reboot
Then a throwable Exception class like this in PHP:
 static::FILE_LIFETIME;
   }
}
Then we could throw a new instance of reboot anytime we want a reboot and just wait for 1 minute max. Obviously this approach can be done on many server side language like Java, Ruby on rails or maybe even programming language like Python. This is an alternative approach to using exec(), and this could be used in other server execution stuffs like IP blocking and Image processing (e.g. making thumbnails) Of course you need to make sure that your execution request system isn't exploitable, but still, it is limited to what you define, so in some sense, it is more secure and error proof. Here's a version I made for my own MVC Framework, ADD MVC
mail();
      die();
   }

   /**
    * Mail subject
    */
   public function mail_subject() {
      return $this->truncated_subject("PHP Request: Reboot: ");
   }

   /**
    * Cancel reboot request
    * Can be used when you catch this exception and want to cancel the reboot request
    *
    * @return bool
    */
   public function cancel_reboot_request() {
      return unlink(static::reboot_request_file());
   }


   /**
    * Check if a reboot request is initiated
    * Checksi if file exists and is less than 5 minutes old
    * @return boolean
    */
   public static function is_reboot_requested() {

      $file = static::reboot_request_file();
      return file_exists($file) && !static::is_reboot_failed();
   }

   /**
    * Checks if the reboot request failed
    * Might mean the cron job failed (permission error? make sure you use root's crontab)
    * Might also mean there's no cron job
    * This relies on the cron job being just less than 5 minutes
    * So if you set the cron job to check like, every 15 minutes, then change the FILE_LIFETIME constant to 900 (15*60)
    *
    * @return boolean
    */
   public static function is_reboot_failed() {
      return (filemtime(static::reboot_request_file())-time()) > static::FILE_LIFETIME;
   }
}
 

Comments (0)


Add a Comment





Allowed tags: <b><i><br>Add a new comment:


A Few Accomplishments

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer non. Adipiscing cubilia elementum integer. Integer eu ante ornare amet commetus.

Possibly broke spacetime

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Terraformed a small moon

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Snapped dark matter in the wild

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer adipiscing ornare amet.

Contact Me

Integer eu ante ornare amet commetus vestibulum blandit integer in curae ac faucibus integer non. Adipiscing cubilia elementum integer. Integer eu ante ornare amet commetus.