running list of bash commands in a file in parallel

1.3k Views Asked by At

I have a simple text file (command_script.txt) that I am executing as a bash script.

The content of command_script.txt is something like this:

#!/bin/bash
some_command -flags input1 output1
some_command -flags input2 output2
some_command -flags input3 output3
...
some_command -flags input1000 output1000

I execute it on the command line (./command_script.txt) and it runs through one line at a time. Is there an easy way I can run 20 lines at a time in parallel?

Thanks!

2

There are 2 best solutions below

1
miro On

By appending "&" at the end of a line a process is launched in background. Hence, the next one is started immediately such that both are running in parallel.

0
Ole Tange On
#!/usr/bin/parallel --shebang -j20
some_command -flags input1 output1
some_command -flags input2 output2
some_command -flags input3 output3
...
some_command -flags input1000 output1000

If the commands are literaly the ones given, you can instead do:

seq 1000 | parallel -j20 some_command -flags input{} output{}