Running commands at once

I have text file contain the following commands

command1 file1_input; command2 file1_output
command1 file2_input; command2 file2_output
command1 file3_input; command2 file3_output
command1 file4_input; command2 file4_output
command1 file5_input; command2 file5_output
command1 file6_input; command2 file6_output
command1 file7_input; command2 file7_output
................
................
................
................
................

I named this file “commands” then I gave it permission using “chmod a+x

I want command 1 to be run, then command 2. Also I want this to be applied on all the files (file1, file2, …. etc) at once. How can I modify the content of this file to do that?

I tried the following but it didn’t work:

(
command1 file1_input; command2 file1_output
command1 file2_input; command2 file2_output
command1 file3_input; command2 file3_output
command1 file4_input; command2 file4_output
command1 file5_input; command2 file5_output
command1 file6_input; command2 file6_output
command1 file7_input; command2 file7_output
................
................
................
................
................
)&

Answer

Make the lines as such:

(command1 file1_input; command2 file1_output) &
(command1 file2_input; command2 file2_output) &
...

And each line will execute its two commands in sequence, but each line will be forked off as parallel background jobs.

If you want the second command to execute only if the first command completed successfully, then change the semicolon to &&:

(command1 file1_input && command2 file1_output) &
(command1 file2_input && command2 file2_output) &
...

Leave a Reply

Your email address will not be published. Required fields are marked *