Cron, which kills the process after 5 hours

I basically have a cron job that runs every night that updates thousands of products in my database.

The reason I run cron at night is because there will be less latency on the servers, because not many people visit the site at this time, cron can work for many days without any interference.

Here's what the cron job command looks like

30 23 * * * /usr/bin/php /var/www/ul/prices_all.php >> /var/www/ul/log/prices_all.txt 

What I would like to know is it possible to create a cron task that kills this process after 5 hours, for example.

 30 05 * * * kill /var/www/ul/prices_all.php[process] 
+5
source share
2 answers

You can do this with a timeout (coreutils):

 30 23 * * * timeout 18000 /usr/bin/php /var/www/ul/prices_all.php >> /var/www/ul/log/prices_all.txt 

It just sets a timeout (18000 seconds = 5 hours) and kills the process if it still works after this time.

Or you can set a timeout in the php file itself:

 <?php set_time_limit(18000); 
+16
source

Yes, you could create a cronjob that killed the process in 5 hours. There are several decent ways to do this. For example, you could write the first script your pid (you can get it with getmypid () ) in a file at startup and then ask the second cron job to read the pid and kill it. It will work, but rather inelegantly.

A more elegant method will set the execution limit in the script itself if your PHP configuration allows this (i.e. is not in safe mode).

0
source

All Articles