Track Child process exit


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Track Child process exit
# 1  
Old 07-26-2010
Track Child process exit

hi,

I have a job that spawns multiple child processes in background.. Catch is i want to wait for some jobs to finish before i spawn more background processes. (each job creates a file and deletes at the end of it . so i don't want start new jobs after x amount of disk size is used up)

now, i setup trap
trap child_exit_handler CHLD
to increment and decrement a counter. but this traps executes only once for any number of child exits. Can somebody shed light what mistake i am doing. or a better way to do this.

here is my actual program.
I wanted to increment / decrement TOTAL_PROC variable . since i can't change this inside child job, i am trying to increment before child starts and decrement when child exits.
Code:
#!/bin/ksh
 
set -o monitor # must set to invoke CHLD signals
 
typeset -i TOTAL_PROC
function do_child
{
echo "ENTER CHILD $PPID with total  $TOTAL_PROC"
sleep 5
echo "Exit CHILD $PPID total $TOTAL_PROC "
exit 0
}
 
function child_exit_handler
{
echo "Enter child handler total $TOTAL_PROC "
(( TOTAL_PROC  -=  1 ))
echo "Exit child handler total $TOTAL_PROC "
}
 
TOTAL_PROC=0
 
LOOP_COUNT=0
MAX_COUNT=$1
 
typeset -x COPID=
 
echo "ENTER WHILE $$ at `date`"
 
while [[ $LOOP_COUNT < $MAX_COUNT  ]]; do
 
echo "LOOP IS $LOOP_COUNT , $MAX_COUNT"
 
(( TOTAL_PROC  += 1 ))
do_child  & COPID="$COPID $!"  # capture PID of coprocess
trap  child_exit_handler CHLD
 
echo "Inside while total $TOTAL_PROC "
(( LOOP_COUNT  += 1 ))
done
echo "Outside while total before wait $TOTAL_PROC "
 
wait
echo "Outside while total after wait $TOTAL_PROC "
echo "EXIT WHILE $$ at `date`"

# 2  
Old 07-26-2010
Your problem is probably that you have your trap inside a do...while loop.
# 3  
Old 07-26-2010
Earlier i had trap command outside loop and still it ran for only one child exit.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to exit from the parent script while the child is running?

hi, i want to call a child shell script from a parent shell script. the child will be running for 5 mins. normally when the child is running, parent will wait till the child completes. so in the above case parent will be paused for 5 mins. is there a way so that the parents does not wait for the... (3 Replies)
Discussion started by: Little
3 Replies

2. Shell Programming and Scripting

Kill child processes when exit

Hi, I have parent script which is invoking multiple child scripts. I would want to kill all the child processes before the parent process exit. > cat ./parent #!/bin/ksh while do . ./child arg1 & if ; then break fi done Is there a way to get the process group id for all the child... (3 Replies)
Discussion started by: midhun19
3 Replies

3. 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

4. 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

5. 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

6. 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

7. Programming

Why does my child process not exit?

Im sure it has something to do with the wait() call, but everything ive tried either leaves me with a zombie or with the exec executing indefinitely. switch(pid = fork()) { case -1:perror("fork failed"); exit(1); case 0: if(key == "cd") { execl("/bin/cd", "cd",... (2 Replies)
Discussion started by: p00ndawg
2 Replies

8. Shell Programming and Scripting

How to track exit status of ftp automation

ftp automation code is ftp -v -n -i $host_name << EOF user $u_name $u_pass bi mput $tar_file bye EOF How to check whether the file is successfully transfered or not. Suppose the user name or password is provided wrongly then the code should track the error and ask the end user to enter... (2 Replies)
Discussion started by: Dip
2 Replies

9. UNIX for Dummies Questions & Answers

Exit from n th child shell

Hi, I am using ksh to write my shell script. I need to create multiple-level of nested sub shells in my script. Lets say I have at n th subshell. My question is how do I come out from there to main login shell. If I use 'exit' command then it is exiting from just one subshell and back to... (4 Replies)
Discussion started by: indra_saha
4 Replies

10. Shell Programming and Scripting

Getting exit status of child in trap handler

Hi, I have a trap problem when calling a child script in the background. I know there are a lot of threads here on the issue of traps and signals, I think I have read all the relevant ones, but still haven't found an answer to my problem. I'm working on Linux or HP, the script as you can see... (4 Replies)
Discussion started by: rimon
4 Replies
Login or Register to Ask a Question