Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SHELL

bash run multiple commands in parallel

# Basic syntax:
# If you want to run a bunch of shell commands simultaneously, you can
# put them in a file followed by " &" and then bash the file, e.g.:
commands_to_run.txt # contains the following lines:
command 1 &
command 2 &
command 3 &

# Running the file with bash will cause all commands to be run in parallel
bash commands_to_run.txt

# Where this can be practical when you want to build up a set of similar 
#	shell commands to e.g. process a set of samples.
# Note,
#	- in Vim, you can type :%s/^/text to add before/ to add text at the
#		beginning of every line and :%s/$/text to add after/ to add text at the
#		end of every line.

# Example usage:
# Say you have a directory of files that you want to process in parallel with
# the same shell command. To do this, I'll often do the following:
# Send filenames to a file:
ls /directory/of/files > commands_to_run.txt

# Initial contents of file:
cat commands_to_run.txt
file1
file2
file3
file4

# Prepend and append relevant parts of the command:
vi commands_to_run.txt
:%s/^/beginning of command/
:%s/$/ &/

# Final contents of file:
cat commands_to_run.txt
beginning of command file1 &
beginning of command file2 &
beginning of command file3 &
beginning of command file4 &

# Run all commands in parallel
bash commands_to_run.txt
Source by saveriomiroddi.github.io #
 
PREVIOUS NEXT
Tagged: #bash #run #multiple #commands #parallel
ADD COMMENT
Topic
Name
5+6 =