22

I have three shell script which I am running as below-

sh -x script1.sh

sh -x script2.sh

sh -x script3.sh

So each script is executed sequentially one at a time after previous one finished executing.

Problem Statement:-

Is there any way I can execute all the three above scripts at same time from a single window? I just want to execute script1, script2, script3 at the same time. If you think of some CRON JOB scheduling script1 at 3 AM, script2 at 3AM, script3 at 3AM (all three scripts at the same time, simultaneously). That's what I need, I need to execute all the three scripts simultaneously.

arsenal
  • 23,366
  • 85
  • 225
  • 331
  • Check out my answer at: http://stackoverflow.com/a/33106658/2032852 I shared a solution to that same problem with a script that allows you to run any number of programs, redirect their outputs to separated log files and wait for them to finish. – Joaopcribeiro Oct 13 '15 at 15:35

4 Answers4

40

you want this?

$ sh -x script1.sh & sh -x script2.sh & sh -x script3.sh &

Update explanation :

  • Run each script in background mode so that next command is run without waiting for current command to complete.
  • '&' makes the scripts run in background so that prompt does not wait for it to complete
  • '&' also can be used to chain commands on one line similar to running commands one by one on command line.
Rohan
  • 52,392
  • 12
  • 90
  • 87
  • Can you explain what this will do? As I am not much familiar with unix stuff. – arsenal Oct 16 '12 at 04:55
  • Rohan - you have a couple of errors there. The error message will look like `bash: syntax error near unexpected token ;`. You should remove both semicolons. & is a valid command separator by itself. Eg, try `echo a &; echo b &; echo c &` vs `echo a & echo b & echo c &` – James Waldby - jwpat7 Oct 16 '12 at 05:00
  • Just for reference: `&` is a *control operator*, in this case used to create a *list* of *simple commands* that are executed in the *background*. – user123444555621 Oct 16 '12 at 05:19
  • Thanks this is working. But suppose If I need to kill this process as it is running in background, how can I do that? – arsenal Oct 16 '12 at 16:33
7

With GNU Parallel you can do:

parallel sh -x ::: script1.sh script2.sh script3.sh

If the scripts are executable then you can even do:

parallel ::: script1.sh script2.sh script3.sh

Watch the intro videos to learn more: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1

10 seconds installation:

$ (wget -O - pi.dk/3 || lynx -source pi.dk/3 || curl pi.dk/3/ || \
   fetch -o - http://pi.dk/3 ) > install.sh
$ sha1sum install.sh | grep 883c667e01eed62f975ad28b6d50e22a
12345678 883c667e 01eed62f 975ad28b 6d50e22a
$ md5sum install.sh | grep cc21b4c943fd03e93ae1ae49e28573c0
cc21b4c9 43fd03e9 3ae1ae49 e28573c0
$ sha512sum install.sh | grep da012ec113b49a54e705f86d51e784ebced224fdf
79945d9d 250b42a4 2067bb00 99da012e c113b49a 54e705f8 6d51e784 ebced224
fdff3f52 ca588d64 e75f6033 61bd543f d631f592 2f87ceb2 ab034149 6df84a35
$ bash install.sh
Ole Tange
  • 31,768
  • 5
  • 86
  • 104
6

The & allows a process to run in the background.

sh -x script1.sh &
sh -x script2.sh &
sh -x script3.sh &
squiguy
  • 32,370
  • 6
  • 56
  • 63
  • Thanks this is working. But suppose If I need to kill this task as it is running in background, how can I do that? – arsenal Oct 16 '12 at 16:35
  • @user1419563 You can look up the `PID` (process ID). Usually what I do is type `ps -ef | grep sh` to find all the shell scripts that are running. From there you can type `kill PID` which is not elegant but works. – squiguy Oct 16 '12 at 18:36
4

Not sure what you are trying to accomplish but you can create a script that calls these 3 or send them to background by adding a "&" at the end.

sh -x script1.sh &
sh -x script2.sh &
sh -x script3.sh &
Baconator507
  • 1,747
  • 2
  • 12
  • 20