views:

33

answers:

2

Hi,

In a script I'd like to monitor the process list in a way that, in order to continue the execution of the script, a certain process has to be started.

I came up with something like:

while ! pgrep "process_to_match"
do
  sleep 10
done;

# the rest of the script

The problem with that script is that if the "process_to_match" is started for less than 10ms the "rest of the script" won't be executed.

An even better solution for me would be to trigger the execution of a script on "process_to_match" launch.

Any ideas? Thanks.

+2  A: 

Can you check in another way that the process has been executed? I mean does this process logs or modifies anything?

If not, you can replace the process by a shell script (rename the process and create a shell with the process file name) that will log something after running the process you are waiting for.

Benoit Thiery
+1. I second this. Some kind of reasonably reliable notification that "process_to_match" has started would be preferably to grepping for it. Perhaps have it create a lockfile with it's pid?
Noufal Ibrahim
Precisely, I can't assume any beahvior of the "process_to_match". I can only rely on its name.
gregseth
Can you replace this process by a shell script that would call your renamed process?
Benoit Thiery
A: 

What is your actual need?

If you know the PID of the process you are monitoring, then you just have to wait for it:

wait $pid

Obtaining this PID is as simple as:

process_to_match & pid=$!
mouviciel
I don't know the PID, I don't control the start of the process_to_match, I just want to start a script when this process is started.
gregseth