I have a file processes.txt
as follows:
process_1.sh
process_2.sh
process_3.sh
I will run the following on the terminal:
cat processes.txt | xargs -L1 -P3 sh
How would this be different if I had a script executable_processes.sh
as follows:
process_1 &
process_2 &
process_3 &
wait
where process_1
, process_2
and process_3
are scripts with execute permissions but performing the same tasks as process_1.sh
, process_2.sh
and process_3.sh
, and I ran the following on the terminal:
sh executable_processes.sh
My colleague told me that the first example (using xargs -L1 -P3
) runs the three processes "truly in parallel", while the second example (sh executable_processes.sh
) that uses &
runs the three processes as background processes but still sends them to the background consecutively, i.e. process_1
is sent to the background first, next process_2
is sent to the background and then process_3
is sent to the background. Therefore he prefers that I use the first example. But my problem with the first approach is that I do not know how to use xargs -L1 -P3
if the three lines in processes.txt
were as follows:
cat input_1.txt | process_1
cat input_2.txt | process_2
cat input_3.txt | process_3
Let's say I modified the file processes.txt
as follows:
input_1.txt | process_1
input_2.txt | process_2
input_3.txt | process_3
And then run
cat processes.txt | xargs -L1 -P3 cat
This throws the error
cat: '|': No such file or directory
I want to be able to run cat input_1.txt
first and the pipe the output to process_1
and so on. But input_1.txt
, |
and process_1
are being handled as arguments to cat
If using &
and xargs -P
are not different from each other would it be more reasonable to simply run a script that contains:
cat input_1.txt | process_1 &
cat input_2.txt | process_2 &
cat input_3.txt | process_3 &
xargs -P
does things "truly in parallel"- even modern CPUs do literally nothing in actual simultaneity. While there is an extremely small difference betweencommand & command & command &
andxargs -P sh 'command'
,xargs
will still at the end of the day be sending one instance ofcommand
at a time into the background to be executed. – DopeGhoti Jul 13 '22 at 14:07cat processes.txt | xargs -L1 -P3 sh -c
with every line ofprocesses.txt
within quotes. I have been asked to keep this approach instead of using&
at the end of each line. – sriganesh Jul 14 '22 at 07:26