I've created a php script that allows me to click a button to restart a PHP script. However, I'm not sure the best way to do it. Here's a snapshot of it: http://i51.tinypic.com/2niz32o.png
I currently have this:
if(isset($_POST['login_restart']))
{
$command = exec("/usr/bin/php /var/www/html/login_server.php >/dev/null &");
$pid = exec("nohup $command > /dev/null 2>&1 & echo $!");
$info = "Login server started...PID: $pid";
}
However, that doesn't seem to work. I need it so when the "Restart" button is pressed, it starts the login server, and keeps it running. I've been using the screen function in SSH, however, I don't want to have to keep logging into SSH to restart the login server. I want to somehow use a process ID so I can check to see if the script is running, and if it's not, it'll allow me to click the "Restart" button.
Thanks.
Is there a particular reason that you want to do this manually and not automatically? Is it not the case that the server should always be restarted?
My advice would be to automate this, either by using
cron
to check the status of your script at regular intervals, orbash
infinite loop script immortality.First create a launcher script to invoke your PHP for convenience, and call it
run_login_server.sh
(don't forget to chmod +x it so it can be executed):Then create
login_server_daemon.sh
to run your script in an infinite loop (again,chmod +x
it to make it executable):N.B. I have not backgrounded the php process in the above
bash
script. It works, because the bash loop will call php each time, and the loop will only iterate again once php has died. Just executelogin_server_daemon.sh
to start the loop (either through an init service or in a detached screen session like you are using now).If your PHP scripts hang, or you want to reload them because you have updated your code, you can simply kill the looped process–
run_login_server.sh
and the bash loop will respawn it.It's as simple as
killall run_login_server.sh
, which you could do via php'sexec
. Note that you need to be careful about the user permissions of who has executed what: if you executelogin_server_daemon.sh
as your_username but php runs as php_username then php will not have permission tokillall
your process.Finally, if you can't choose between
cron
and thescript
approaches, here are some factors to consider:The script should live forever, and will only die if 1) explicitly killed, 2) bash somehow trips and dies on a while loop, which I doubt would happen, and 3) a machine-wide catastrophe happens, in which case your little bash script stopping is the least of your worries. A bonus with the script is that restart is immediate after php (or whatever you want to call in the infinite loop) dies.
cron
has a the problem that it can only check once a minute at its most frequent setting, if you really care about immediate recovery. It has the additional annoyance that if you decide to stop the script, you also have to remove it from your crontab or it will just come back to life.