Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a cron job kill last cron execution?

I have a cron job the executes a PHP script. The cron is setup to run every minute, this is done only for testing purposes. The PHP script it is executing is designed to convert videos uploaded to the server by users to a flash format (eg... .flv). The script executes fine when manually doing it via command line, however when executing via cron it starts fine but after one minute it just stops.

It seems that when the next cron is executed it "kills" the last cron execution. I added the following PHP function:

ignore_user_abort(true);

In hopes that it would not abort the last execution, I tested setting the cron to run every 5 minutes, which works fine, however a conversion of a video may take over 5 minutes so I need to figure out why its stoping when another cron is executed.

Any help would be appreciated.

Thank you!

EDIT: My cron looks like:

*/1 * * * * php /path_to_file/convert.php
like image 487
Patrik Johansson Avatar asked May 12 '09 09:05

Patrik Johansson


People also ask

How do I know if a cron job ran last?

Using the grep command, you can view the log to see the last time when the specific script in the cron job was executed. If the cron job does not produce a visible output, then you would need to check to see if the cron job has actually taken place.

Does cron job overlap?

In particular, cron has no way to prevent two executions of the same task from overlapping. If we want to avoid concurrent execution of our tasks, we need to implement it ourselves within the task's script. In this tutorial, we'll learn two ways of preventing task overlapping, based on process detection and using a .

Can 2 cron jobs run at the same time?

Technically it's possible to run both imports at the same time via cron jobs, but it's not recommended – actively running imports are resource-intensive and could cause your server to struggle.

How do you kill a cron job?

Stop 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

I don't think cron kills any processes. However, cron isn't really suitable for long running processes. What may be happening here is that your script tramples all over itself when it is executed multiple times. For example, both PHP processes may be trying to write to the same file at the same time.

First, make sure you not only look in the php error log but also try to capture output from the PHP file itself. E.g:

*/1 * * * * * php /path/to/convert.php & >> /var/log/convert.log

You could also use a simplistic lockfile to ensure that convert.php isn't executed multiple times. Something like:

if (file_exists('/tmp/convert.lock')) {
    exit();
}

touch('/tmp/convert.lock');
// convert here
unlink('/tmp/convert.lock');
like image 96
Sander Marechal Avatar answered Oct 17 '22 18:10

Sander Marechal