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?
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
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)