0

I have a PHP script that I want to run forever; from starting up server until shutdown.

PHP script

<?php
require_once("connection.php"); // I am connecting to MySQL with PDO

while(true) {
//some of my code here
....
....
//code ended
sleep(5);
}
?>

My /etc/init/myscript.conf file

description "Endless PHP loop"
start on startup
stop on shutdown
respawn
chdir /var/www/html/
exec php -f script.php

I also had try

script
exec php -f script.php
end script

When I run: start myscript from terminal script is running without any problem but if server is rebooted myscript wont run again. Also sometimes script is stop running (I don't know why) and wont start running again.

I am googling for two days and I did not find solution. Maybe I do not know what to look after.

Could it be that script fails because apache, mysql or php startin up? Is there option to delay script 30 sec after startup?

mandza
  • 330
  • 9
  • 24
  • sorry to ask but... what does you script do? – Julio Soares Nov 02 '15 at 15:39
  • A syntax error will keep your script from running (as you're not including actual code there's no way of ruling that out) as would a fatal error in connection.php. Have you got error reporting and/or logging on? What happens when you try to run the script from the commandline? – GordonM Nov 02 '15 at 15:40
  • Does it need to run every 5 seconds? – Fluinc Nov 02 '15 at 15:40
  • Don;t you need to `script` before and `end script` after `chdir /var/www/html/ exec php -f script.php` for upstart? – Fluinc Nov 02 '15 at 15:44
  • I have two scripts one of them needs to run every 5 other one every 2 sec. @Fluinc chdir and path I have try every combination. – mandza Nov 02 '15 at 16:07
  • @JulioSoares script is connection to another server with API sending info from server where it is running. Because of that delay of 1min is sometime a problem. Also sometimes script running time is longer that 5 sec (very rare) because of that 5sec cron job is not right solution for me – mandza Nov 02 '15 at 16:15

2 Answers2

1

Try changing your upstart script to:

description "Endless PHP loop"

start on startup
stop on shutdown

respawn

script
    sleep 30
    exec php -f /var/www/html/script.php
end script

If you wanted to it with cron you need to remove the endless loop from PHP and add these cron entries:

* * * * * /usr/bin/php -f /var/www/html/script.php &> /dev/null
* * * * * (sleep 5;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 10;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 15;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 20;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 25;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 30;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 35;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 40;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 45;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 50;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
* * * * * (sleep 55;/usr/bin/php -f /var/www/html/script.php &> /dev/null)
Fluinc
  • 491
  • 2
  • 10
  • script ... end script is approach that I had already try. Also full path is something I have try but script is not braking because I can run it from terminal manually. – mandza Nov 02 '15 at 15:49
  • did you try the full path and the `script end script` together? – Fluinc Nov 02 '15 at 15:50
  • I had try them together too, same result – mandza Nov 02 '15 at 16:00
  • Try adding `exec sleep 30` before `exec php...` – Fluinc Nov 02 '15 at 16:16
  • exec sleep 30 is working. if you add this comment into you answer I can accept it as a right one. – mandza Nov 02 '15 at 16:29
  • i added it to the answer. – Fluinc Nov 02 '15 at 16:34
  • I marked it as correct answer but I had to unmarked it. Because myscript.conf is now running when I type status myscript but it does not execute my php script. Maybe two exec is making trouble – mandza Nov 02 '15 at 23:26
  • 1
    I solved it. As I aspect two exec was a problem. I removed exec before sleep and it worked like a charm.. I am accepting this answer as correct one. It would be nice @Fluinc if you update answer again – mandza Nov 03 '15 at 00:10
-1

It is a bad idea to do it like this.

PHP is designed to run on request, and not endless. Maybe it can be done, but who knows what memoryleaks you will introduce?

I strongly suggest you look into cronjobs.

Just crontab your code and run it every 5 seconds.

Here is an example to run each minute. I don't think you can go down to 5 seconds using cron.

* * * * * cd /home/yourdir/public_html/admin/ && php -q /home/yourdir/public_html/admin/updatedb.php

Have a look here for another approach: Running a cron every 30 seconds

Community
  • 1
  • 1
Erwin Moller
  • 2,375
  • 14
  • 22
  • Doesn't really answer the question, and running in an endless loop is a legitimate use case in some circumstances. – GordonM Nov 02 '15 at 15:41
  • @GordonM Cron is the way to go in most circumstances. It is a poor idea to leave PHP running endlessly. – Erwin Moller Nov 02 '15 at 15:44
  • if you read my question you will see that I am already using this. Only problem is that script wont start on startup. When I run it manually it is running for weeks. – mandza Nov 02 '15 at 15:45
  • 2
    cron jobs only go down to every minute. – Fluinc Nov 02 '15 at 15:45
  • @ErwinMoller cron job is not solution for my problem. Because I need to run script every 5 or 2 sec. – mandza Nov 02 '15 at 15:51
  • Did you read the end of my respons? WIth the hyperlink to "Running a cronjob every 30 seconds"? If you create (yes it is ugly) 30 cronjobs, you can do it every 5 seconds. But if Fluinc response works for you, go with that: much cleaner. – Erwin Moller Nov 02 '15 at 15:59