0

I have a php script on a Linux server that run like:

nohup php myscript.php > /dev/null &

This script needs to be restarted every hour. How can i do that automatically?

Note: Script has a functions that runs indefinetely. So when it starts, it must remain open. So before the script re-run again, it must be killed.

user1002601
  • 289
  • 3
  • 17

2 Answers2

2

Start the script by a shell script that runs an endless loop and waits for the process to finish. Since you also have an endless loop in your PHP script this will only be if your script is stopped.

Next you write the PID of the script into a text file. You should get the PID with this: http://php.net/manual/en/function.getmypid.php

Next step is to set up a cronjob that runs every hour. This cronjob reads the PID .txt file, deletes it and kills the process.

Since the shell script mentioned in the beginning also has an endless lopp it will restart the php process directly.


Example scripts:

Starter Script:

#!/bin/bash
while true
do
  php myscript.php > /dev/null
done

save it e.g. as start.sh and set chmod +x to be able to execute it.

Now you can start it with: nohub ./start.sh &

in .php you do something like this at the beginning:

file_put_contents("/tmp/yourscript.pid", getmypid());

And heres the cron script as bash:

#!/bin/bash
PID=`cat /tmp/yourscript.pid`
rm /tmp/yourscript.pid
kill -9 $PID

you can also to the cron script with php:

<?php
$pid=file_get_contents("/tmp/yourscript.pid");
unlink("/tmp/yourscript.pid");
shell_exec("kill -9 $pid");
virtualmarc
  • 513
  • 1
  • 4
  • 14
  • Thanks for the detailed answer. So the shell script at the beginning will be started from terminal. But other shell script that reads the PID from file, deletes it and then kill the process will run from the cron. Here we will kill the PHP script but the shell script will make it run again. Great. But the problem is i don't know bash scripting, now i need to look bash examples to do that. – user1002601 Sep 03 '13 at 10:07
2

To add a cronjob use :

    crontab -e

then

   0 * * * *   timeout 3600  nohup php myscript.php > /dev/null &

timeout param will kill the process after '3600' second in this example

UPDATE

also you can use

  set_time_limit(3600);

in the beginning of your php file that will kill it after 3600 seconds

trrrrrrm
  • 10,028
  • 22
  • 79
  • 128
  • This script will run 1 hour then kill it and re-run again because of the hourly cron. But what if it kills the process as soon as it start again? This 60 minute cron and 60 minute timeout will align correctly to re-run script every-hour? How can we be sure about that? What if i put 3599 or 3590 second to be sure? – user1002601 Sep 03 '13 at 10:20
  • Also thanks for the simple answer. I have other scripts like that. And they all in different directories. Writing the full path to crontab will be sufficient for them? – user1002601 Sep 03 '13 at 10:25
  • Yes you can do something like decrease 3600 but when the cronjob run again it will init a new process so killing the previous one wont be a problem, but you can decrease it to make sure that only one is running at a given time. – trrrrrrm Sep 03 '13 at 10:28
  • ya writing full path wont be a problem at all. – trrrrrrm Sep 03 '13 at 10:29
  • Using the crontab you showed to me i am getting "bad minute error" in cron.Why is that? – user1002601 Sep 03 '13 at 13:47
  • The scripts are in different directories. Like /path/to/script1/myscript1.php and /path/to/script2/myscript2.php When i put timeout 3600 nohup /usr/bin/php /path/to/script1/myscript1.php to crontab it doesn't work. Similary when i write directly to terminal timeout 3600 nohup /usr/bin/php /path/to/script1/myscript1.php it does not work again. Only if i cd to that directory and run directly from there like timeout 3600 nohup php myscript1.php it works. Why is that? – user1002601 Sep 11 '13 at 10:11