Running multiple processes with bash

23 February, 2020

I found myself in this scenario, I need 2 processes to run at the same time and if one process finishes, then the other should terminate as well.

The solution to this is as follows: 1st. You can use the & ampersand to run a command in the background

2nd. by adding


This waits for either background job to terminate. Follow that with:

pkill -P $$

which will kill any background process that's running.

npx cypress run  --browser electron --headless --spec "cypress/integration/apps/tests.spec.js" | tee  some_log_name.txt &
npx cypress run --browser chrome --headless --spec "cypress/integration/apps/mustalwaysrun.spec.js"

wait -n
pkill -P $$

tee will read from the standard input and writes to standard output.

sources: unrelated but worth a read: