How to wait for the process and know if the exit code of the sub process
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to wait for the process and know if the exit code of the sub process
I am trying to find out in shell how to wait for the background process to finish before continous, and how to know the exit code for the background process which have finish.
Basically, if I do the following, I can wait for the background processes
sleep, but the problem is that, if I kill one of the sleep process, the
wait command do not return the kill status of the background process, it
always return 0, but I want the script to return the exit code as fail if
one of the subprocess fail.
#!/bin/csh -f
sleep 10 &
sleep 30 &
wait
If I do the following in bash shell, it only wait for one job and return
failing of the job in only one background process, I need to check for 2 process.
#!/bin/bash
sleep 10 &
wait %1
How could I wait for 2 background process and return their exit code ?
wait [n ...]
Wait for each specified process and return its termination sta-
tus. Each n may be a process ID or a job specification; if a
job spec is given, all processes in that job's pipeline are
waited for. If n is not given, all currently active child pro-
cesses are waited for, and the return status is zero. If n
specifies a non-existent process or job, the return status is
127. Otherwise, the return status is the exit status of the last process or job waited for.
Now I meet another problem, when I kill the pid of the child process. The child process also start off some other process. It just happen when I kill the top level child process alone, all its child process under are not kill. since I do not use "exec" command to run the child process, its child child process give new pid.
Does it have a way know all the child process under the parent process, and kill them all.
Do I need to do a while loop my self to search for all the child process ?
Thanks a lot it really work. Now, when I have a lot of bg job, I don't need to look at ps one byone, and kill them one by one.
But I have one questions, when the shell will start a new pgid ?
Is that anytime when we type in a shell with interactive shell window, it will be given a new gpid ?
If there is a script calling this script RUNFILE, when the process of RUNFILE is being killed,
since the trap subroutine try to kill the pgid,it will kill the parent process as well ?
If it is the case, does I have a method to start a new pgid when I call on a new script, for example, this RUNFILE. or it will always following the parent pgid.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.