LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-08-2011, 11:08 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
wait


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"?

#!/bin/bash
find / -name 'file*log' > /home/search_results.log
<wait>
echo "done"
 
Old 03-08-2011, 11:11 AM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Um... That's default behaviour, isn't it?

Code:
[joshua@joshua-desktop:~/test]$ cat test2.sh                      (08-03 17:11)
#!/bin/bash

find / -name 'file*log' > search_results.log
echo "done"
[joshua@joshua-desktop:~/test]$ sh test2.sh  

<lots of other permission messages>

find: `/root': Permission denied
find: `/usr/lib64/mozilla/extensions': Permission denied
find: `/tmp/tmux-0': Permission denied
find: `/lost+found': Permission denied
find: `/etc/openvpn/certs': Permission denied
find: `/etc/openvpn/keys': Permission denied
find: `/etc/cups/ssl': Permission denied
find: `/etc/polkit-1/localauthority': Permission denied
find: `/etc/samba/private': Permission denied
done

Last edited by Snark1994; 03-08-2011 at 11:12 AM. Reason: Added example output
 
Old 03-08-2011, 11:16 AM   #3
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
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?
 
Old 03-08-2011, 12:00 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
Old 03-08-2011, 12:04 PM   #5
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
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?
 
Old 03-08-2011, 03:14 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.
 
Old 03-08-2011, 03:17 PM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Yes, see the Job Control Builtins section in the Bash Reference Manual for details.

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.
 
Old 03-08-2011, 04:47 PM   #8
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
Old 03-08-2011, 06:05 PM   #9
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by theNbomr View Post
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.
Special Parameters
 
Old 03-08-2011, 06:27 PM   #10
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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.

--- rod.
 
Old 03-08-2011, 09:10 PM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by theNbomr View Post
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:
Code:
~$ false &
[1] 11881
~$ 
[1]+  Exit 1                  false
~$ wait $! ; echo $?
1
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Should I wait for 7.04 dickgregory Ubuntu 7 04-15-2007 02:23 PM
Should I get 9.2 or wait out for 10? eBopBob Mandriva 10 03-10-2004 09:49 PM
9.2 or better wait for 10? tijs Mandriva 4 02-07-2004 12:11 PM
Can't wait!!! nny0000 Programming 2 01-17-2004 04:18 AM
I cant wait TazLinux General 6 05-31-2003 08:00 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration