child process state


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers child process state
# 1  
Old 12-23-2007
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.

Last edited by smreddy; 12-23-2007 at 04:47 AM..
# 2  
Old 12-23-2007
Quote:
Originally Posted by smreddy
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.
# 3  
Old 12-23-2007
child process state

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?
# 4  
Old 12-23-2007
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

Code:
if child
then
     dothisothertask
fi

and the child looks like

Code:
thirdstage()
{
        doalltherest
}

doinitialwork

if status-met
then
     thirdstage &
     exit 0
fi

exit 1

# 5  
Old 12-23-2007
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
# 6  
Old 12-23-2007
Quote:
Originally Posted by matrixmadhan
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.
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
# 7  
Old 12-23-2007
The following is a special christmas present for porter:

Quote:
Originally Posted by porter
(a) a process terminating yielding an exit code

(b) reading of a stream
(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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (6 Replies)
Discussion started by: naveeng
6 Replies

2. UNIX for Advanced & Expert Users

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (1 Reply)
Discussion started by: naveeng
1 Replies

3. BSD

Process remians in Running state causing other similar process to sleep and results to system hang

Hi Experts, I am facing one problem here which is one process always stuck in running state which causes the other similar process to sleep state . This causes my system in hanged state. On doing cat /proc/<pid>wchan showing the "__init_begin" in the output. Can you please help me here... (0 Replies)
Discussion started by: naveeng
0 Replies

4. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

5. Emergency UNIX and Linux Support

signal between parent process and child process

Hello, everyone. Here's a program: pid_t pid = fork(); if (0 == pid) // child process { execvp ...; } I send a signal (such as SIGINT) to the parent process, the child process receive the signal as well as the parent process. However I don't want to child process to receive the... (7 Replies)
Discussion started by: jackliang
7 Replies

6. Shell Programming and Scripting

script to get child process for a process

!/bin/sh pid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $1}') sid=$(ps -Aj | grep MSTRSvr | grep -v grep | awk '{print $3}') ps -s "$sid" I am not able to get the desired output it says process list error if i use watch ps -s "$sid" it considers only the first session id (5 Replies)
Discussion started by: schippada
5 Replies

7. UNIX for Advanced & Expert Users

When a process will go to 'D' state?

I'm using "Linux hostname 2.6.28-15-generic #49-Ubuntu SMP Tue Aug 18 18:40:08 UTC 2009 i686 GNU/Linux" All the client machines will use Thin-client ,I will use my laptop for working and I will mount my home directory from server to my laptop. If I open the firefox in my laptop the... (1 Reply)
Discussion started by: ungalnanban
1 Replies

8. Shell Programming and Scripting

[KSH/Bash] Starting a parent process from a child process?

Hey all, I need to launch a script from within 2 other scripts that can run independently of the two parent scripts... Im having a hard time doing this, if anyone knows how please let me know. More detail. ScriptA (bash), ScriptB (ksh), ScriptC (bash) ScriptA, launches ScriptB ScirptB,... (7 Replies)
Discussion started by: trey85stang
7 Replies

9. Shell Programming and Scripting

How to make the parent process to wait for the child process

Hi All, I have two ksh script. 1st script calls the 2nd script and the second script calls an 'C' program. I want 1st script to wait until the 'C' program completes. I cant able to get the process id for the 'C' program (child process) to make the 1st script to wait for the second... (7 Replies)
Discussion started by: sennidurai
7 Replies

10. UNIX for Dummies Questions & Answers

Process State

If your process makes a system call, then while the system call code is being run in the kernel, is your process READY, RUNNING or BLOCKED? (1 Reply)
Discussion started by: ianlow
1 Replies
Login or Register to Ask a Question