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?
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)
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
Related
I setting on the host this cronjob and it work but, I need local and dont know how it work
my code is:
1 step
open terminal and type
crontab -e
2 step
write this code
* * * * * wget --spider -O - /Applications/XAMPP/xampfiles/htdocs/mysite.local/index.php/cron/comment >/dev/null 2>&1
3 step
save and close file.
but not work... where is problem?
You need to use the absolute path of the script.
* * * * * /usr/bin/wget --spider -O - http://insta.local/cron/comment >/dev/null 2>&1
I have good .php file when run directly via address bar browser.
But not process perfectly when run via cron job, may be it's just process first query msyql/first loop.
there is any special configuration for it?
My cron setup is
0 * * * * wget --spider -O - http://domain.com/cronjob >/dev/null 2>&1
See below example that is run your file on per minute
* * * * * /usr/bin/php /var/www/html/<your-file-name>
< and > are removed in <your-file-name>
I want to run a .php every 10 min with a cron job on Ubuntu.
This is my crontab:
*/10 * * * * php -f /var/www/html/gapi/src/test2.php >/dev/null 2>&1
And this is in the log file:
CRON[9994]: (root) CMD (php -f /var/www/html/gapi/src/test2.php >/dev/null 2>&1)
In this php is an api call, and I can see the api calls live at the dashboard of the api provider, so I know the php is not running every 10 mins.
I set the file permission to 755, what else can I do to make it work?
Updated Crontab:
*/10 * * * * php -f /var/www/html/gapi/src/test2.php
Try requesting the file through your web server rather than calling the script via the command line PHP interpreter.
*/10 * * * * wget -q -O /dev/null http://localhost/gapi/src/test2.php
(-q to suppress output, -O /dev/null to redirect file output so it doesn't save it)
or using curl instead:
*/10 * * * * curl --silent http://localhost/gapi/src/test2.php
The URL will depend on how your server is set up - you say it works through your browser at the moment so just use the same URL in the cron file.
I have written a crontab to call a function with following syntax to run every 5 minutes,
*/5 * * * * curl http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583
in the function, I have redirected to google with redirect('http://www.google.com');for test but it does not seem to work.Any suggestions..
If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab.
*/5 * * * * lynx http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583.php
or
*/5 * * * * /usr/bin/curl http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583
or
*/5 * * * * /usr/bin/wget -q http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583
instead of redirect('http://www.google.com'); trying using this:
header('Location: http://www.google.com');
Use curl or wget if you want to just "ping" a url.
*/5 * * * * /usr/bin/wget -O /dev/null http://localhost/domain/path/front_orders/recursive_pay/F0C473D9BD583
You can also use one of
/usr/bin/wget --spider http://localhost/... (if doing a HEAD call will do)
/usr/bin/curl http://localhost/... > dev/null 2>&1
If you use
/usr/bin/wget -q http://localhost/...
I think you will end up putting a copy of the result in whatever the 'current' directory is (differs based on which user's cron is running).
I am trying to execute a php script in conjunction with the Sleep command from Crontab. Here is the relevant code:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root HOME=/
# run-parts
* * * * * sleep 5 && root /usr/bin/php /path/to/the/php/file.php
But it won't execute at all. Any hints?
Remove root from sleep 5 && root /usr..
If you want to run it as root, the correct format is:
* * * * * root sleep 5 && /usr/bin/php /path/to/the/php/file.php