How to run multiple commands in parallel and see output from both?
Specifically, I want to run
lsyncd lsyncd.luaAnd
webpack --progress --color -wWhich are both long-running processes. I want to see the output from both, interlaced in my terminal. It doesn't matter if the results are a bit jumbled, I just like to see that they're doing what they're supposed to.
Also, I want it to kill both processes when I press Ctrl+C.
I'm trying
parallel ::: 'lsyncd lsyncd.lua' 'webpack --progress --color -w'which seems to be working, but I can't see any output even though when I run those commands individually, they output something.
35 Answers
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
% 1 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 4Spend 20 minutes reading chapter 1+2 of GNU Parallel 2018 (online, printed). Your command line will love you for it.
2& to the rescue. It launches the two processes in parallel.
lsyncd lsyncd.lua & webpack --progress --color -w
This will do the trick.
Didn't read the kill part. A ctrl+C here would only terminate the second one. The process preceding the & runs in the background although it outputs on the stdout.
The shortest way to terminate both processes is:
1. Type Ctrl+C once. It kills the foreground process.
2. Type fg and type Ctrl+C again. It brings the background process to foreground and kills it too.
HTH!
9You have more options. Run the first command and press Ctrl-Z. This puts the command to wait in the background. Now type bg and it will run in background. Now run the second command and press Ctrl-Z again. Type bg again and both programs will run in background.
Now you can type jobs and it will print which commands are running in background. Type fg <job number> to put program in foreground again. If you omit the job number it will put the last job in the foreground. When the job is in foreground you can stop it with Ctrl-C. I don't know how you would stop both with one Ctrl-C.
You can also add & at the end which puts it running in the background immediately without Ctrl-Z and bg. You can still bring it in foreground with fg.
There is a light-weight way of doing it without installing anyting.
Step 1: Create a script file runner.sh
#!/bin/bash
lsyncd lsyncd.lua &
P1=$!
webpack --progress --color -w &
P2=$!
wait $P1 $P2Step 2: Make it executable
chmod +x runner.shStep 3: Run it
./runner.shIt works like cream
Explanation:
some_command &means run some_command and put it in backgroundlsyncd lsyncd.lua &means runlsyncd lsyncd.luaand put this process in background.
Note: this works with Debian based Linux distros like Ubuntu