Hi I have a script mainscript.sh
In main script I have multiple chile script.
child1.sh
child2.sh
child3.sh
rm -rf /home/bdata/batch/*
I am running my mainscript.sh which will run all child jobs in parallel.
mainscript.sh
child1.sh &
child2.sh &
child3.sh &
rm -rf /home/bdata/batch/*
4th statement runs before completing all the execution.
Is there any way I can control the execution of last line after finishing above 3 scripts in parallel.
Simply tell the shell to wait until the children are all dead:
child1.sh &
child2.sh &
child3.sh &
wait
rm -rf /home/bdata/batch/*
This is one solution:
#!/bin/bash
./child1.sh &
./child2.sh &
./child3.sh &
Use ./ or full path to the script.
If a command is terminated by the control operator &, the shell
executes the command in the background in a subshell. The shell does
not wait for the command to finish, and the return status is 0. If you want to execute all child-scripts before exiting the script then add wait at the end (as #Mark Setchell wrote below).
Related
I have below scripts ready with me -
1.sh:
echo "Good"
sleep 10
echo "Morning"
2.sh:
echo "Whats"
sleep 30
echo "Up"
script1.sh:
sh1.sh &
sh2.sh &
script2.sh:
echo "Hello world"
Requirement:
Execute script1.sh and do not wait for its completion or failure i.e., let the script run in background As soon as script1.sh is triggered the very next second execute the script2.sh.
./script1.sh
./script2.sh
Challenge:
./script2.sh keeps on waiting for completion of . ./script1.sh.
Like ./script2.sh I have lot of scripts to be run one after another but they should never wait for completion of ./script1.sh
Thanks,
B.J.
Just as youdid in 1.sh, you should append & after script1.sh:
#! /bin/bash
./script1.sh &
./script2.sh
exit 0
This will create a background process of script1.sh and continues in the main thread with script2.sh.
Usually, it a good practice not to leave background processes (unless they are long running servers, daemons, etc.). Better to make the parent script wait for all the children. Otherwise, you might have lot of orphan processes, which may use resources and have unintended consequences (e.g., open files, logging, ...)
Consider
#! /bin/bash
script1.sh &
script2.sh
script3.sh
wait # wait for any backgrounded processs
One immediate advantage is that killing the main script will also kill running script1 and script2. If for some reason the main script exit before all background childs are terminated, they can not be easily stopped (other then killing them by PID).
Also, using ps/pstree will show system status in clear way
I want to run command_a and command_b in parallel, and wait for both of them finishing to start another command_c. Is there a simple command/idiom in shell that allows me do that?
Can you simply do
$ command_a &
$ command_b &
$ wait
(the ampersand puts the shell job in the background)
From https://ss64.com/bash/wait.html
If n is not given, all currently active child processes are waited
for, and the return status is zero.
I making CI build program.
I using three script
first is for build. second is for asset build. The last is for running the first and second scripts.
I want to wait for the first and second script until they done in The last script.
Here is the test script.
The first script
#!/bin/sh
sleep 1
echo test2
exit 0
The second script
#!/bin/sh
sleep 1
echo test3
exit 0
The last script
#!/bin/sh
open -a /Applications/Utilities/Terminal.app ./screenTest2.sh &
open -a /Applications/Utilities/Terminal.app ./screenTest3.sh &
wait
echo test
how can I wait new terminals die or scripts. I'm newly in OS X. So can you explain easily that solution?
Bash instructions are sequential just remove '&' character at the end of each line. Each instruction will execute one after the other. You can safely remove wait as well it will not be necessary to wait.
I am launching an a jar application from apple script.
do shell script quoted form of jvmpath & " -jar -XstartOnFirstThread -Dapple.awt.UIElement=true -Dfile.encoding=UTF8 " & quoted form of jarpath & " " & quoted form of parameters
The script keeps running till i quit my jar application.
But i am required to launch another application form shell script.
Since i am doing this in a Cocoa app i want to do this in the background.
Thus, can i launch multiple scripts in multiple instances of terminal (so that they aren't blocking one another).
Note: I tested it by running the command in two different terminal windows, works as expected.
See Technical Note TN2065, specifically the answers to the questions "I want to start a background server process; how do I make do shell script not wait until the command completes?" and "I have started a background process; how do I get its process ID so I can control it with other shell commands?".
The AppleScript code to run two commands in the background would look like this:
set pid1 to do shell script command1 & " &> /dev/null & echo $!"
set pid2 to do shell script command2 & " &> /dev/null & echo $!"
The pid1 and pid2 variables will be set to the process ids of the two commands. You can later check whether the commands are still running by calling a function like this one:
on isProcessRunning(pid)
try
do shell script "kill -0 " & pid
set isRunning to true
on error
set isRunning to false
end try
return isRunning
end isProcessRunning
i have a shell script run.sh.
cd elasticsearch-1.1.0/
./bin/elasticsearch
cd
cd RBlogs/DataFetcher/
mvn clean install assembly:single;
cd target/
java -jar DataFetcher-0.0.1-SNAPSHOT-jar-with-dependencies.jar
Here if second line(./bin/elasticsearch) executes it runs infinite time, so the next lines will not execute. So what i need is to perform the next lines after 10 seconds. But
cd elasticsearch-1.1.0/
./bin/elasticsearch
sleep 10
cd
cd RBlogs/DataFetcher/
mvn clean install assembly:single;
cd target/
java -jar DataFetcher-0.0.1-SNAPSHOT-jar-with-dependencies.jar
This also will not execute next lines because ./bin/elasticsearch will not complete its execution in 10seconds. So how can i solve this problem? Please help.
Adding & at the end of ./bin/elasticsearch will cause the process to run in a subshell, freeing the current shell up for the next commands.
./bin/elasticsearch &
Change this in your second version of the script and things should run like you want them too.
More information can be found from man bash
If a command is terminated by the control operator &, the shell
executes the command in the background in a subshell.
The shell does not wait for the command to finish, and the return status is 0.
you may try to put it in background
& can do this for you
./bin/elasticsearch &
If you simply want elasticsearch to run in the background while the rest of the script progresses, just use &:
cd elasticsearch-1.1.0/
./bin/elasticsearch &
sleep 10
cd
cd RBlogs/DataFetcher/
However, if you want to run elasticsearch for at most 10 seconds, killing it if necessary, then proceeding with the rest of the script, you need something a little more complicated:
cd elasticsearch-1.1.0/
./bin/elasticsearch &
pid=$!
sleep 10
kill -0 $pid && kill $pid
cd
cd RBlogs/DataFetcher/
As other answers mentioned, you can
use ./bin/elasticsearch & to run the command in
background.
record the process id of the
command run in background by using child_pid=$! and then stop the
process by using kill $child_pid after some time to implement a
timeout mechanism.
Meanwhile, you also can synchronize another operation with the command run in background by using wait command. An example bellow:
./bin/elasticsearch &
# do something asynchronously here
wait # wait for accomplishment of ./bin/elasticsearch
# do something synchronously here