I basically have a cron job which runs every night that updates thousands of products in my database.
The reason I run the cron job at night is because there will be less latency on the servers as not many people visit the site during this time, the cron job can pretty much run on for days without any interference.
Here is 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 would it be possible to create a cron job which kills this process after 5 hours e.g.
30 05 * * * kill /var/www/ul/prices_all.php[process]
It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .
You could use a second cron job at 09pm to start a second program that tells the first program to terminate. There are a number of ways to do this. One of the easiest might be to have the second program touch terminate. txt in a convenient place.
The answer is from https://crontab.guru/every-2-hours.
You can stop a single cron job by removing its line from the crontab file. To do that, run the crontab -e command and then delete the line for the specific task. Alternatively, you can stop the cron job by commenting it out in the crontab file.
You can do this with timeout (coreutils):
30 23 * * * timeout 18000 /usr/bin/php /var/www/ul/prices_all.php >> /var/www/ul/log/prices_all.txt
It simply sets a timeout (18000secs = 5 hours) and kills the process if it is still running after that time.
Or you can set a timeout in the php file itself:
<?php set_time_limit(18000);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With