23 February, 2020

Running multiple processes with bash

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 js>& ampersand to run a command in the background

2nd. by adding

wait-n

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 $$

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

sources: https://unix.stackexchange.com/questions/231676/given-two-background-commands-terminate-the-remaining-one-when-either-exits unrelated but worth a read: https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/