Reputation: 148
I want to check whether process is ended or not with php. Please tell me what is the better way to solve this problem.
I have consider several idea for this, for example:
pattern1:
$res=array();
exec('ps auxww | grep "some.php some_param 1" | grep -v grep', $res);
//if $res is empty, process may have ended.
or pattern2:
$res = array();
exec('pgrep -fx "some.php some_params 1"', $res);
currently I think pgrep is the better way. but is there any other method?
Upvotes: 1
Views: 746
Reputation: 63538
If you know the PID of the process (which is a good idea - have it write its pid into a file), then you can check if it is alive with
posix_kill(PID,0)
Which returns TRUE only if the process is alive.
However, this is a bit bogus, as it might return TRUE if some other process has appeared with the same PID. PID reuse is quite likely, especially if the OS was rebooted since the process started.
A better way is to have the process lock a file (with e.g. flock) and then you can see if the lock is still present (by trying to lock the file yourself with FLOCK_NB)
Upvotes: 1
Reputation: 19118
The convenient way is to create a PID file in /tmp on the start of the process, e.g: myprocess.pid. This file contains the process id of the created job. Then your monitoring tool has an easy way to check whether the process runs or don't.
Upvotes: 1