![]() |
bash scripting
Hello,
I am working on a bash script that does some initial setup for, then launches several applications (in the background). These applications (4 in total) execute simultaneously and coordinate with each other. The all continue to execute until closed by the user. What I'd like to know is if it is possible to program the bash script that launches these apps to "lie and wait" for all of them to exit? (So that some cleanup can be done.) Here's what my script looks like... Code:
#!/bin/bash |
Absolutely.
Something like this would work: Code:
pid_list='' |
jhwilliams has a great solution. Another is making use of the wait command.
Code:
command_foo & |
Quote:
|
I know jhwilliams said "something like this" but I just wanted to point out that his example, as written, would be an infinite loop. Specifically:
Code:
[ -n "$pid_list" ]I would also vote for wait. Though, perhaps a merger of the efforts of jhwilliams and GamezR2EZ: Code:
#!/bin/bashIt waits for each process ID in sequence. There is a danger in this approach. Say process #1 runs for days, but process 4 runs for a few seconds. There is a chance that process 4's process ID will be re-used by the system. So, it's possible that the script would end up waiting on a process it did not spawn itself. To avoid that, you probably need to use a polling loop (like in jhwilliams original post) that checks each process ID in the list once every X seconds/minutes/hours. Then, if the process is gone, remove that ID from your checklist. EDIT: Corrected a few things... I referenced "waitpid" instead of "wait" which is incorrect. "waitpid" is a function in C... not a shell command. :) |
Wow! Thanks to all for the replies!
|
Is there any reason not to use wait without any process ID? It will wait for all spawned processes if none is given.
NB: There is the command jobs to list all spawned background processes and jobs -p to list all process IDs at once. |
word count program help
Hi,
I have recently started working in bash script and currently creating short programs and I could not find the path to create a new thread, I just tried to create the program for counting the words in a file but its not running successfully. program: #!/bin/bash num=`wc -l test1.sh` echo $num if [ "$num" -gt "1" ] then echo "this is gt 1" fi other information: the number of lines in test1.sh file is 3. error: verma@verma-Lenovo-G560:~/shell-scripts$ sh test.sh 3 test1.sh [: 8: Illegal number: 3 test1.sh Could someone please help on same,thanks in advance. Regards Sandeep Verma |
Please check the output of the wc command, this needs to be trimmed.
NB: In the forum overview “Forum Tools” (near the right side) => “Post New Thread” |
Quote:
|
The output of wc includes the filename, and there is no option to leave it out, hence a reference to the plain line count could be:
Code:
${num%% *}Instead of wc you could also use awk to get the number of line directly without a filename: Code:
awk 'END {print NR}' test1.sh |
Quote:
Many thanks reuti! This resolved the problem!! |
| All times are GMT -5. The time now is 05:51 AM. |