How to run multiple commands in parallel and see output from both?

Using parallel (in the moreutils package):

parallel -j 2 -- 'lsyncd lsyncd.lua' 'webpack --progress --color -w'

Since the parallel process runs in the foreground, hitting CTRL+C will terminate all the processes running on top of it at once.

  • -j: Use to limit the number of jobs that are run at the same time;
  • --: separates the options from the commands.
% parallel -j 2 -- 'while true; do echo foo; sleep 1; done' 'while true; do echo bar; sleep 1; done'
bar
foo
bar
foo
bar
foo
^C
%

GNU Parallel defaults to postponing output until the job is finished. You can instead ask it to print output as soon there is a full line.

parallel  --lb ::: 'lsyncd lsyncd.lua' 'webpack --progress --color -w'

It avoids half-line mixing of the output:

parallel -j0 --lb 'echo {};echo -n {};sleep {};echo {}' ::: 1 3 2 4

Spend 20 minutes reading chapter 1+2 of GNU Parallel 2018 (online, printed). Your command line will love you for it.

Tags:

Command Line