user3408779
user3408779

Reputation: 997

How to find the first cron job process complete or still running?

I hvave a cron job that is doing some updates. After completion of that process i need to run another cron job for inserting that updated records into another table.For this I am trying this script.

 $output = shell_exec('ps -C php -f');
if (strpos($output, "php do_update.php")===false) { 
  shell_exec('php insert_process.php');
}

But it is not doing any thing and i am getting ps is not internal / external command. So how can I find that first cron job execution is completed or not?Once that is executed then i will run the second cron for inserting data. Any holp would be greatly appreciated.

Upvotes: 0

Views: 57

Answers (2)

Arun Kumaresh
Arun Kumaresh

Reputation: 6311

you can use flock on running the do_update.php add a lock to it and in

`insert_process.php` run the process if your previous lock is released

in your do_update.php add this

$fp = fopen("lock.txt", "r+");

if (flock($fp, LOCK_EX)) {

  //do your process

  flock($fp, LOCK_UN); //release the lock

}

in your insert_process.php add this

$fp = fopen("lock.txt", "r+");

if (flock($fp, LOCK_EX | LOCK_NB)) { // checks for the lock

// do your process
}

Upvotes: 1

Michal Bieda
Michal Bieda

Reputation: 939

You could use some kind of a lock file.

First file:

  1. at the beginning of the first script create empty file on the disk called lockfile.txt (or any other name)
  2. at the end remove the file from the disk

Second file

  1. check if the file called lockfile.txt exists, if not - run the code

Upvotes: 1

Related Questions