ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Hi, if I am writing a script and want to wait for one of the lines to finish executing before moving on, how do I do this? For example, how do I wait for the find command in the following script to be completely finished before printing out the "done"?
Okay I must admit I didn't test that but typed it in a hurry trying to make a point but I guess that was a bad example. Basically I have come across some scripts within which some of the commands moved on before finishing. Am wondering how I can put a wait or something to halt it before moving on. Do I make sense?
As Snark1994 points out, that is the default behavior. If you want a process to run as a background process in a shell script, you have to explicitly launch it as such by appending the '&' character to the commandline. This is probably the case that you saw, without realizing it.
Some applications are capable of launching themselves as daemon processes, by detaching themselves from the controlling terminal. These are intended to run persistently, and you probably don't want to wait for them to complete.
Ah yes correct the cases I saw were executed in the background. Okay so in this case, how do I make it such that the next command in the script will wait for the previous line to complete in background? I know I can simply change it by dropping the "&" at the end but is there a way?
I cannot think of as way to do that without running the risk of a race problem. The bash wait command requires a PID, and you would have to do a bit of shell gymnastics to locate the PID of the process of interest. In the meantime, the process may have already terminated.
--- rod.
For example, if you wanted to make sure all the background tasks the current shell has started, just use the wait Bash built-in (no arguments, that's it).
(theNbomr, wait does not need a PID. And if you wish to select one from the background tasks, use jobs and e.g. sed to pick out the interesting ones, and feed the job ID's to wait.)
Last edited by Nominal Animal; 03-08-2011 at 03:19 PM.
Okay, I actually knew you could supply a job ID. But how will you get a job ID to pass to wait (assuming you don't want to wait on all running processes)? Seems to me that you still end up with the possibility of the process terminating before you've figured out wait you're waiting for.
In any case, the whole concept seems to be little more than of academic interest, unless someone can explain why you would want to do it that way, or what problem it solves that couldn't be solved the 'usual' way.
I cannot think of as way to do that without running the risk of a race problem. The bash wait command requires a PID, and you would have to do a bit of shell gymnastics to locate the PID of the process of interest. In the meantime, the process may have already terminated.
--- rod.
You can just use $! to get the PID.
Quote:
!
Expands to the process id of the most recently executed background (asynchronous) command.
Yes, of course. But you would still have to do something like
Code:
mycommand
#
# Somewhere in here, 'mycommand' may terminate
#
wait $!
You could argue that waiting for a non-existant PID is harmless, except it produces the error message 'bash: wait: pid XXXXXXX is not a child of this shell'.
I just don't see the point of the exercise when there is a better way to do it.
You could argue that waiting for a non-existant PID is harmless, except it produces the error message 'bash: wait: pid XXXXXXX is not a child of this shell'.
I just don't see the point of the exercise when there is a better way to do it.
No, the pid remains in the process table as a zombie process until the parent waits for it:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.