Does a cron job kill last cron execution?

后端 未结 3 523
生来不讨喜
生来不讨喜 2020-12-30 08:40

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 con

相关标签:
3条回答
  • 2020-12-30 09:19

    cron itself won't stop a previous instance of a job running so, if there's a problem, there's almost certainly something in your PHP doing it. You'll need to post that code.

    0 讨论(0)
  • 2020-12-30 09:33

    No, it will not. You can keep a second process from running by creating a lock file that the script checks for on each run. If the file exists, it does not run. This should also, if appropriate, be used in conjunction with a maximum execution time so that one process does not stall future executions indefinitely. The lock file can just be an empty plain text file called /tmp/foo.lock.

    0 讨论(0)
  • 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');
    
    0 讨论(0)
提交回复
热议问题