Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cron job that kills a process after 5 hours

Tags:

php

cron

crontab

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]
like image 952
mk_89 Avatar asked Jan 31 '13 22:01

mk_89


People also ask

What is the use of * * * * * In cron?

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 .

How do I stop a cron job after a certain time?

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.

How do I schedule a cron job every 2 hours?

The answer is from https://crontab.guru/every-2-hours.

How do you kill a cron job?

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.


1 Answers

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);
like image 92
Green Black Avatar answered Sep 30 '22 07:09

Green Black