Page 1 of 1

how to kill the process of cron job.

Posted: Wed Jun 29, 2005 5:29 am
by genux33
if i set up a cron job for a script.. if the next cronjob run after first run, how should i detect that the first cronjob is not finished and avoid the execution of the 2nd run of the cron job.

Posted: Wed Jun 29, 2005 5:34 am
by djot
-
guess you can handle process ids to do that. anyone?

Since my knowledge of that is limited, I would keep a logfile, where I would write "finished" to, when the first process is over. So next start of the cron you may check whether status is "finished" or "running..."

djot
-

Posted: Wed Jun 29, 2005 5:50 am
by genux33
djot wrote:-
guess you can handle process ids to do that. anyone?

Since my knowledge of that is limited, I would keep a logfile, where I would write "finished" to, when the first process is over. So next start of the cron you may check whether status is "finished" or "running..."

djot
-
Your idea go like
1. Check if the status in the log file is finished.
2. If status = running , then do nothing,
3. if status = finish, Update the status = running,
4. After update status = running, then do whatever needed,
5. After finishing everything. update the status = finish.

Is that what you mean?

Is there any function that can determine the process id of the current running cron job in php or linux commands?

Posted: Wed Jun 29, 2005 5:57 am
by djot
got my idea ... as sa(i)d, I don't know how to handle process ids.
see http://de2.php.net/pcntl

Posted: Wed Jun 29, 2005 7:35 am
by Weirdan
you may use lock files. Lock file is an empty file on which you obtain the lock to prevent concurrent execution. Something like this:

Code: Select all

define('LOCKFILE', '/tmp/my.lock');
if(!defined('LOCK_EX')) {
    define('LOCK_EX', 2);
}

if(!defined('LOCK_NB')) {
    define('LOCK_NB', 4);
}

if(!defined('LOCK_UN')) {
    define('LOCK_UN', 3);
}

$fp = fopen(LOCKFILE, 'w');
if( !flock($fp, LOCK_EX|LOCK_NB) ) {
    die("Failed to obtain the lock");
}

// your script here

flock($fp, LOCK_UN);
fclose($fp);
There are other ways as well, e.g. semaphores