child process state

Hi all,
I have one requirement,I have two shell programs one is parent and the other one is child . from parent script i need to execute/trigger/call child script as background job. my requirement is after calling child script i want the child process information i.e PID of child weather it is still running or not?

I am written the following code in parent shell which returns 2 everytime i.e the output is "child not running".

/tmp/child.sh &
ChildPIDRunningStatus=`ps -ef | grep $! | grep -v grep | wc -l`

if [ $ChildPIDRunningStatus -gt 2 ]; then
echo "child is running"
else
echo "child is not running"
fi

your suggestions are needed.

If you are doing the test immediately after starting it and doing the test one only then you might as well assume that it *always* starts and is still running due to the independent nature of processes.

Are you going to repeatedly check the status of the child process, if so grab the $! value into a variable and check against that.

Hi,
Thanx for reply, I am not testing the child status repeatedly.testing only once after calling that as you said it should run.the child script contain some logic that runs for a while. depending upon that status i need to do further processing. to go ahead the child status should return it is running. if not i am unable to go ahead.
i tried with that what you suggested to assign that into a variable. in that case also same result i am getting.

do u have any other ideas?

Do you mean

  1. start child

  2. test if child has reached a certain status

  3. if so do another task

The problem you have is what time interval/synchronisation are you doing between 1 and 2?

Imagine the child starts but takes a while to reach the certain status?

Why not have the child do

  1. do initial work to determine the magic status

  2. if the status is met,
    2.1 spawn another child to continue the child tasks
    2.2 return the status as the exit code

Then all you need to do is

if child
then
     dothisothertask
fi

and the child looks like

thirdstage()
{
        doalltherest
}

doinitialwork

if status-met
then
     thirdstage &
     exit 0
fi

exit 1

Is the status logged in a file?

Then it would be much more easier to identify the step from where other process can continue processing.

But again the granularity between sleeping and checking for any log messages is purely experimental and you have to give a random number for that.

parent process - starts the child process
child process logs the info in a file
controller process checks whether necessary message has been logged into file or not
if not then continue sleeping
if yes continue with the next steps

You can't use sleep to reliably synchronise between processes, you need to use events and state. In UNIX the the best two events are

(a) a process terminating yielding an exit code

(b) reading of a stream

The following is a special christmas present for porter:

(c) use a semaphore or a semaphore-like file

(d) use the coprocess-facility

(e) with the necessary glue-logic you can even use sleep, like matrixmadhan suggested, but i have to agree it would be unnecessary effort given the number of other options.

That makes five. ;-))

Have a nice christmas, porter. ;-))

bakunin

(c) I ignored semaphores for this threads as we're looking at simple shell solutions.

(d) That is a combination of (a) and (b).

(e) this is what we were trying to avoid.

(f) I avoided mentioning signals as they are non trivial in a shell program.

... et bonne annee!

Exactly right ! :slight_smile:

But since its a scripting problem I presented a much simpler solution to implement and use.