Long cron running time via wget / curl?
I am working on cron jobs for my php application and am planning to use cron via wget / curl. Some of my php cron jobs can take 2-3 hours. How to allow php to run 2-3 hours from the cron tab? Is it good practice to run such lengthy work orders via wget / curl cron? Anyone have any experience? I also have an email queue and have to run every 10 seconds, but the cron tab is minute level. Any suggestions for this case?
Thanks for reading.
a source to share
When you use wget / curl, you are requesting a page from the web server. Each web server will have a timeout period, so it may time out.
Also some of the hosting providers can stop the process running in certain minutes (mainly to control rogue threads).
Therefore, it is inappropriate to schedule the use of wget / curl if the job takes more than a few minutes.
Try planning it with a real planner. You can run php from command line
php [options] [-f] [-] [Arg ...]
The php command must be in the path.
a source to share
At the beginning of your script, you can use the following so that PHP does not effectively waste time:
set_time_limit(0);
What could be wiser if crontab will run the script every 24 hours, set the timeout to 24 hours so that you don't run two copies at the same time.
set_time_limit(24*60*60);
Crontab only allows minimal execution, because more often than not you have to run the script - there are startup / shutdown costs that make faster scheduling inefficient.
If your application needs to check the queue every 10 seconds, the best strategy might be one lengthy script that does this check and uses sleep () occasionally to stop itself from hogging system resources.
On a UNIX system, such a script should actually run as a daemon - look at the PEAR System_Daemon package for an easy way to do this.
a source to share
I am facing an issue with a long PHP script executed via cron and wget. The script exits after 15 minutes due to the default timeout in wget.
However, I believe this method actually works if you have configured the correct timeouts. To run a PHP script you need to set an unlimited execution time. Your best bet is to set this at the beginning of your long script.
set_time_limit(0);
When using wget, you also need to remove your timeout by passing -T0. eg.
wget -q0 -T0 http://yourhost/job.php
Be very careful not to overload the server with lengthy scripts.
a source to share