How to wait for a grand child to finish?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to wait for a grand child to finish?
# 1  
Old 12-16-2014
Ubuntu How to wait for a grand child to finish?

Hello all,

I have a very basic question. I have a requirement where in, I have a main process which forks a child process, which execs and runs a c executable corresponding to a daemon. In the c executable corresponding to a daemon, as everyone does, I fork another child process, and as part of child, run the daemon functionality by its parent exiting, so that the child(daemon) becomes a child of init. Now, from the main process(grand parent), I would like to wait for the daemon process(grand child).

Just to explain more clearly, I have a process p1, which forks a new process p2. In p2 functionality, I fork another process p3 which execs and runs a daemon, p2 exits to make p3 a child of init. Now, from p1, I want to wait for p3. I can not use waitpid in the grand parent process to wait for the grand child I believe. How to proceed?

I would be very glad if you can help me.

Thanks in advance,
Sai
# 2  
Old 12-17-2014
You could write a waiting loop, like this:

Code:
start_p2 &
while [ $(ps -fe | grep start_p3 | wc -l) -le 1 ] ; then
     print - "process not started yet"
     sleep 1
done
print - "process start_p3 has started"

Notice that you need to query for more than one process because "grep start_p3" will be found by "grep start_p3" as a process too.

Alternatively add a "grep -v grep" to the pipeline and change the comparison to "-lt 1".

I hope this helps.
# 3  
Old 12-17-2014
Or you try this
Code:
start_p2 &
while [ $(ps -fe | grep [s]tart_p3 | wc -l) -le 1 ] ; then
     print - "process not started yet"
     sleep 1
done
print - "process start_p3 has started"

# 4  
Old 12-17-2014
If the parent PID becomes 1, then your are no longer the grandparent. You could watch for it in a loop if you can determine the PID to look for, for example you could write the PID to a file in /var, which is common.



Robin
# 5  
Old 12-17-2014
I find the stdout/stderr are great at tracking progeny processes:
Code:
( ... ) 2>&1 | cat >log_file

When all processes that did not redirect both stdout and stderr exit, then stdin on cat goes to EOF and it exits.
These 3 Users Gave Thanks to DGPickett For This Post:
# 6  
Old 12-17-2014
Quote:
Originally Posted by bakunin
You could write a waiting loop, like this:

Code:
start_p2 &
while [ $(ps -fe | grep start_p3 | wc -l) -le 1 ] ; then
     print - "process not started yet"
     sleep 1
done
print - "process start_p3 has started"

Notice that you need to query for more than one process because "grep start_p3" will be found by "grep start_p3" as a process too.

Alternatively add a "grep -v grep" to the pipeline and change the comparison to "-lt 1".

I hope this helps.
I wouldn't bat that the own process is always present in the proc table.

Last edited by MadeInGermany; 12-17-2014 at 08:38 PM..
# 7  
Old 12-18-2014
Ubuntu

Thank you all for your replies. But, I forgot to post another requirement of mine. I need to capture the return status of the grand child and the signals it received at the time of the daemon(grand child) exiting. Can you suggest a solution with this additional requirement?

Thank you all in advance,
Sai
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Wait till any one child process finishes

Hi I am facing a problem in my ksh. My main script is calling 3 different child process in the background. I am using wait to finish all and then submit another 3 child processes. Now what i want is , whenever any one child process finishes ,i want to submit next one.so that parallel 3... (2 Replies)
Discussion started by: Sangu
2 Replies

2. Shell Programming and Scripting

How make parent to wait from child process?

Hi all, I am starting mgen5 for sometime depends on input from a file, in a child process. now I want to make parent to wait in this child process till mgen5 finishes, or timeout happens. could anyone please tell me how to make parent to wait in child process in shell script? thanks... (2 Replies)
Discussion started by: girijajoshi
2 Replies

3. Shell Programming and Scripting

How to wait for tar to finish before sending the archive through ftp?

Hey all, I want to automate tarring a directory then using ftp to transfer the files over. I was able to put the commands together but what I'm noticing is that only the very first file is being tarred and then transferred. tar cvpf new.backup sourceAbove is the command I'm using which works... (4 Replies)
Discussion started by: Keepcase
4 Replies

4. Shell Programming and Scripting

How to WAIT for jobs in each group to finish?

I have the shell script to call a Perl routine and pass the Informatica WorkFlow name to it. Jobs in each group executes in background do not seem to wait at all. How do I make it to WAIT for the prior group to complete before execute the next group of jobs? Sample of the jobs flow: { ... (6 Replies)
Discussion started by: lv99
6 Replies

5. Programming

Parent,child wait,signal

Hello. I want to make a child do some stuff,wait,then the parent does some stuff and then child does some stuff and waits again.I have made the following but it does not work.Can anybody help me? pid1 = fork(); if (pid1 == -1) { perror("Can't create child\n"); ... (18 Replies)
Discussion started by: Cuervo
18 Replies

6. Shell Programming and Scripting

Make cron wait for the child process

I am trying to find a list of files and writing it to a text file. Based on the machine performance the file writing will be slow at certain time. The code to find file and redirecting the output to text file is on a shell script /usr/bin/find $SEARCH_DIR -daystart \( \( -name 'KI*' -a... (4 Replies)
Discussion started by: nuthalapati
4 Replies

7. UNIX for Advanced & Expert Users

how to make a parent wait on a child shells running in background?

Hi I have a shell script A which calls another 10 shell scripts which run in background. How do i make the parent script wait for the child scripts complete, or in other words, i must be able to do a grep of parent script to find out if the child scripts are still running. My Code: ... (1 Reply)
Discussion started by: albertashish
1 Replies

8. Shell Programming and Scripting

How to wait for the subprocess to finish in tcl

Hi All Here i have a piece of code, set filename "./GopiRun.sh" #I need to wait here until the GopiRun.sh is completed how do i achive this exit. (1 Reply)
Discussion started by: nathgopi214
1 Replies

9. Programming

Howto spawn multiple child processes and wait?

As far as I can tell, the bash wait command waits for a logical "AND" of all the child processes. Assuming I am coding in C: (1) What is the function I would use to create multiple bash child process running perl? (2) What is the function I would use to reinvent the bash wait command so I... (4 Replies)
Discussion started by: siegfried
4 Replies

10. UNIX for Advanced & Expert Users

How can I wait for PID to finish in another shell

Have a need to schedule programs that can run after other programs are completed. Here's the catch: 1) The list of programs will not always be the same (kind of a plug-n-play deal) 2) The invoking shell may not be the same as the shell of the program being waited on In other words, I need... (2 Replies)
Discussion started by: superdelic
2 Replies
Login or Register to Ask a Question