how to kill the process of cron job.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
genux33
Forum Newbie
Posts: 18
Joined: Sun Apr 10, 2005 8:22 am

how to kill the process of cron job.

Post 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.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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
-
genux33
Forum Newbie
Posts: 18
Joined: Sun Apr 10, 2005 8:22 am

Post 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?
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

got my idea ... as sa(i)d, I don't know how to handle process ids.
see http://de2.php.net/pcntl
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
Post Reply