Problem determining a PID with ps
Hi,
In a bash script, I ran into a problem using ps. It seems to me, that it
takes some time before a newly created process is detected by ps.
Here is what I do: In a bash script, I start a programm using su,
something like
nice su myuser -c mycommand &
SUPID=$!
Because I might have to later send a signal to the mycommand process
later, I then try to determine ist PID:
CMDPID=$(ps -f --ppid $SUPID -C mycommand | awk 'NR == 2 { print $2 }')
To my surprise, CMDPID often was empty. I wrapped the above line into a
loop and found that it might take 2 or 3 attempts until ps finds the
process and CMDPID is set.
Now, here are my questions:
- Is this a pure timing issue, i.e. should 'sleep 1' before the call to
ps reliably solve the problem?
- Is there a better way to determine the PID of the mycommand process?
Thanks, Malte
Reply to: