open-cli-tools / concurrently

Run commands concurrently. Like `npm run watch-js & npm run watch-less` but better.
https://www.npmjs.com/package/concurrently
MIT License
7k stars 228 forks source link

How to combine with other node command #438

Closed michaelsync closed 1 year ago

michaelsync commented 1 year ago

Let's say I have the following commands.

I want to kill the command "two" after a specific time so I have a script file called timeout.js that includs this code setTimeout(() => process.exit(1), 360000);

and then I run like that.

npm run one && concurrently -k "node ./timeout.js" "npm run two" && npm run three

It never reaches to "npm run three" because concurrently always kills the process. How do I tell to kill the processes in the scope which is "npm run two" and don't kill "npm run three" which comes after &&?

paescuj commented 1 year ago

The exit code of concurrently will be 1 so bash never runs the command after &&. If it is really such a simple use case, I'd probably go without concurrently and instead use the command timeout, for example. If there are other reasons for you to stay with concurrently, you could adjust the command to:

# to always run "npm run three"
npm run one && concurrently -k  "node ./timeout.js" "npm run two"; npm run three

# or to run "npm run three" only if concurrently wasn't successful (timeout reached and therefore killed)
npm run one && concurrently -k  "node ./timeout.js" "npm run two" || npm run three

See also https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Lists.