in reply to Executing Systems Calls with Care
my $running=`ps –ef | grep ‘$pse_id’ |grep –v grep |wv –l`;
FWIW using a shell pipeline is nice and concise, but there are problems with it if you really care about detecting if it is working properly.
First you aren't checking for errors at all from this command. Second, even if you were, some shells do not return the error from the earlier commands in a pipeline (e.g. if ps returned an error it would appear that the process was gone when in fact it could still be running). Third, you aren't being precise in what you want grep to find. For example, what happens if $pse_id is something like 100. Then this command would match other processes like with '100' anywhere in the 'ps -ef' (i.e. different PIDs like 10037, part of the command name, user name, parent PID, etc). In this case the best way to find out if a process is still running is to remember it's pid and then either use 'waitpid()' or use something like 'kill 0, $pid;'.
YMMV, but if I really want to check things like 'ps' output like this I tend to use a form of ps that I know the exact format of where the PID is in the line (e.g. "ps -o <fmt> ...") and I parse the output myself within perl.
|
|---|