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, or bash 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):
#!/bin/bash
/usr/bin/php /var/www/html_login_server.php > /dev/null
Then create login_server_daemon.sh to run your script in an infinite loop (again, chmod +x it to make it executable):
#!/bin/bash
while :
do
./run_login_server.sh # or any command line to be executed forever
done
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 execute login_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's exec. Note that you need to be careful about the user permissions of who has executed what: if you execute login_server_daemon.sh as your_username but php runs as php_username then php will not have permission to killall your process.
Finally, if you can't choose between cron and the script 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.
Related
I have a simple question, i searched and I couldn't find a solution.
I have a simple shell script that run a small php code every 2 seconds, I wrote it and save as a file:
$ cat every-2-seconds.sh
#!/bin/bash
while true
do
php /home/account/domains/domain.co.il/public_html/my-php-script.php
sleep 2
done
Now, i need that this script will always run on background, but I also need that it will run on startup, Just like a service, it should always run in background, and I never want to start it manually (of course, if something happen and it will stop, i should be able to start it manually)
I heard about nohup, but its not a service right? and I can start it on startup.. :(
Can you help me on this??
You can make your script run with this line of code (assuming you are in the directory with your script)
nohup every-2-seconds.sh &
The & will run this as a background task and nohup will keep the process running even after you've disconnected from your session.
To handle starting it on reboot you need to add this command to your crontab
crontab -e
#reboot /path/to/every-2-seconds.sh > /dev/null
In the crontab you need to specify the full path. You can change /dev/null to the file you want output to go to (assuming you want the output)
I wrote a shell script in linux to check if one of my program (say programA) is running, if it stopped, it will restart the program.
ok, I also have a php script which hav start & stop button to start and stop the same program from the server side. If the program is already run by the shell script, clicking on start button will NOT run multiple of the same program.
THE PROBLEM IS: if PHP script works fine by itself. But the PHP script cannot close the program if it is run by the shellscript. Is this a permission issue or something that I havent been aware of? (I already did chmod 777 programA btw...)
UPDATE:
in my PHP script, it calls exec("kill -9 PID_of_programA") to kill the program.
I tried to change it to $r = shell_exec("kill -9 PID_of_programA") and echo $r gives me nothing...
You are probably running your program with a user that has no privileges to close other programs... Have you tried to run as super user?
I have a PHP script that has been running as a cron job. The script uses the DB to see if it has anything to do, and to make sure its brethren are not already running.
I'd like to run the PHP script as a daemon with upstart.
I've set up my /etc/init/super-mailer.conf file as this:
description "super mailer"
author "Rob Nugen"
start on startup
stop on shutdown
respawn
exec sudo -u www-data php -f /var/www/super-mailer/scripts/mailer.php
I execute sudo start super-mailer and it runs once.
It doesn't run again, though. Why not?
I've also tried replacing the exec sudo line with
script
sudo -u www-data php -f /var/www/clubberia-mailer/scripts/mailer.php
end script
Do I need to change my PHP script to loop? How do I tell upstart to keep starting the script?
A daemon is a type of program that does not stop until told so. However, your script terminates itself. So yes, you need to make a loop in your script, that will re-run it every time.
However, keep in mind that just making a loop and executing your script again and again, might make it consume many CPU cycles. So, you might consider calling a function like usleep in every iteration to make the deamon a little less CPU-consuming. So, for example, you let your script run every 2 seconds.
My project calls for 3 php scripts that are run with if-else conditions. The first script is loaded on the index page of the site to check if a condition is set, and if it is, it calls for the second script. The second script check to see if other conditions are set and it finally calls for the last script if everything is good.
Now I could do this by just including the scripts in the if statement, but since the final result is a resource hogging MySQL dump, i need it to be run independently of the original trigger page.
Also those scripts should continue doing their things once triggered, regardless of the user actions on the index page.
One last thing: it should be able to run on win and nix.
How would you do this?
Does the following code make any sense?
if ($blah != $blah-size){
shell_exec ('php first-script.php > /dev/null 2>/dev/null &');
}
//If the size matches, die
else {
}
Thanks a million in advance.
UPDATE: just in case someone else is going through the same deal.
There seem to be a bug in php when running scripts as cgi but command line in Apache works with all the versions I've tested.
See the bug https://bugs.php.net/bug.php?id=11430
so instead i call the script like this:
exec("php-cli mybigfile.php > /dev/null 2>/dev/null &");
Or you could call it as shell. It works on nix systems but my local windows is hopeless so if anyone run it on windows and it works, please update this.
I would not do this by shell exec because you'd have no control over how many of these resource-hogging processes would be running at any one time. Thus, a user could go click-click-click-click and essentially halt your machine.
Instead, I'd build a work queue. Instead of running the dump directly, the script would submit a record to some sort of FIFO queue (could be a database table or a text file in a dir somewhere) and then immediately return. Next you'd have a cron script that runs at regular intervals and checks the queue to see if there's any work to do. If so, it picks the oldest thing, and runs it. This way, you're assured that you're only ever running one dump at a time.
The easiest way I can think is that you can do
exec("screen -d -m php long-running-script.php");
and then it will return immediately and run in the background. screen will allow you to connect to it and see what's happening.
You can also do what you're doing with 'nohup php long-running-script.php', or by writing a simple C app that does daemonize() and then execs your script.
For a website, I need to be able to start and stop a daemon process. What I am currently doing is
exec("sudo /etc/init.d/daemonToStart start");
The daemon process is started, but Apache/PHP hangs. Doing a ps aux revealed that sudo itself changed into a zombie process, effectively killing all further progress. Is this normal behavior when trying to start a daeomon from PHP?
And yes, Apache has the right to execute the /etc/init.d/daemonToStart command. I altered the /etc/sudoers file to allow it to do so. No, I have not allowed Apache to be able to execute any kind of command, just a limited few to allow the website to work.
Anyway, going back to my question, is there a way to allow PHP to start daemons in a way that no zombie process is created? I ask this because when I do the reverse, stopping an already started daemon, works just fine.
Try appending > /dev/null 2>&1 & to the command.
So this:
exec("sudo /etc/init.d/daemonToStart > /dev/null 2>&1 &");
Just in case you want to know what it does/why:
> /dev/null - redirect STDOUT to /dev/null (blackhole it, in other words)
2>&1 - redirect STDERR to STDOUT (blackhole it as well)
& detach process and run in the background
I had the same problem.
I agree with DaveRandom, you have to suppress every output (stdout and stderr). But no need to launch in another process with the ending '&': the exec() function can't check the return code anymore, and returns ok even if there is an error...
And I prefer to store outputs in a temporary file, instead of 'blackhole'it.
Working solution:
$temp = tempnam(sys_get_temp_dir(), 'php');
exec('sudo /etc/init.d/daemonToStart >'.$temp.' 2>&1');
Just read file content after, and delete temporary file:
$output = explode("\n", file_get_contents($temp));
#unlink($temp);
I have never tried starting a daemon from PHP, but I have tried running other shell commands, with much trouble. Here are a few things I have tried, in the past:
As per DaveRandom's answer, append /dev/null 2>&1 & to the end of your command. This will redirect errors to standard output. You can then use this output to debug.
Make sure your webserver's user's PATH contains all referenced binaries inside your daemon script. You can do this by calling exec('echo $PATH; whoami;). This will tell you the user PHP is running under, and it's current PATH variable.