Skip to content

How to Take Parallel Jobs to Background in One Line

  • by
Suppose you're a SA. In the beginning of the year 2016, you have to execute multiple system analysis reports parallelly for every month last year. In the first place, you execute the following 1 + 12 commands like the followings.
     1  cd ~/reporting
     2  nohup ./system_analysis_monthly.sh '201501' &
     3  nohup ./system_analysis_monthly.sh '201502' &
     4  nohup ./system_analysis_monthly.sh '201503' &
     5  nohup ./system_analysis_monthly.sh '201504' &
     6  nohup ./system_analysis_monthly.sh '201505' &
     7  nohup ./system_analysis_monthly.sh '201506' &
     8  nohup ./system_analysis_monthly.sh '201507' &
     9  nohup ./system_analysis_monthly.sh '201508' &
    10  nohup ./system_analysis_monthly.sh '201509' &
    11  nohup ./system_analysis_monthly.sh '201510' &
    12  nohup ./system_analysis_monthly.sh '201511' &
    13  nohup ./system_analysis_monthly.sh '201512' &

Yes, they all work, but look so cumbersome. Is there any way to do this in only one line? Of course we can.

In normal situation, we can concatenate all commands with a semicolon ";" to separate all commands like this:
     1  cd ~/reporting; nohup ./system_analysis_monthly.sh '201501' &; nohup ./system_analysis_monthly.sh '201502' &; nohup ./system_analysis_monthly.sh '201503' &; nohup ./system_analysis_monthly.sh '201504' &; nohup ./system_analysis_monthly.sh '201505' &; nohup ./system_analysis_monthly.sh '201506' &; nohup ./system_analysis_monthly.sh '201507' &; nohup ./system_analysis_monthly.sh '201508' &; nohup ./system_analysis_monthly.sh '201509' &; nohup ./system_analysis_monthly.sh '201510' &; nohup ./system_analysis_monthly.sh '201511' &; nohup ./system_analysis_monthly.sh '201512' &
Mostly, it works, but this time, it didn't work. The shell returned an error:
-bash: syntax error near unexpected token `;'
Let's try to put the & symbol to the end of the line:
     1  cd ~/reporting; nohup ./system_analysis_monthly.sh '201501' ; nohup ./system_analysis_monthly.sh '201502' ; nohup ./system_analysis_monthly.sh '201503' ; nohup ./system_analysis_monthly.sh '201504' ; nohup ./system_analysis_monthly.sh '201505' ; nohup ./system_analysis_monthly.sh '201506' ; nohup ./system_analysis_monthly.sh '201507' ; nohup ./system_analysis_monthly.sh '201508' ; nohup ./system_analysis_monthly.sh '201509' ; nohup ./system_analysis_monthly.sh '201510' ; nohup ./system_analysis_monthly.sh '201511' ; nohup ./system_analysis_monthly.sh '201512' &
It did take the job to the background and was doing jobs, but sadly, it's sequentially executed.

Actually, we don't have to add any separator for separating commands, because & is already a separator. So just concatenate them with white spaces except the first one.
     1  cd ~/reporting; nohup ./system_analysis_monthly.sh '201501' & nohup ./system_analysis_monthly.sh '201502' & nohup ./system_analysis_monthly.sh '201503' & nohup ./system_analysis_monthly.sh '201504' & nohup ./system_analysis_monthly.sh '201505' & nohup ./system_analysis_monthly.sh '201506' & nohup ./system_analysis_monthly.sh '201507' & nohup ./system_analysis_monthly.sh '201508' & nohup ./system_analysis_monthly.sh '201509' & nohup ./system_analysis_monthly.sh '201510' & nohup ./system_analysis_monthly.sh '201511' & nohup ./system_analysis_monthly.sh '201512' &
We did it! They are all in one line, and executed in the background parallelly.

Leave a Reply

Your email address will not be published.