I have a server I want to run (on one HTTP port), and then another script (which runs (temporarily) on another port).

I'm using a pipe in order to let these run concurrently, and brackets to group the commands such that sleep occurs first (the second script accesses the server started in the first, so there needs to be some time for the first to be set up).

It works fine, with the server getting started and the sleep and test file executing correctly thereafter, but the terminal process does not exit automatically after the test as it does when just running the script.

npm run start-json-server | { sleep 1; node './test/index-cjs.js' ; }

What do I need to do to allow a natural exit (preferably without going through the trouble of finding the process and killing it)?

Update: I realized it was still running (duh) because the first script, as a server, doesn't terminate... But is there a way I can get it to terminate, again, preferably without finding its PID?

  • Which terminal process you want to exit automatically?
    – ploth
    Oct 1, 2018 at 6:12
  • The start-json-server one--once the node test script is finished. Oct 1, 2018 at 6:14

1 Answer 1


A pipe is not used for starting concurrent processes. It does do that, but it's mainly used for setting up a data stream between two stages of a pipeline.

Instead of your pipeline, use

npm run start-json-server &

sleep 1

node ./test/index-cjs.js

kill "$server_pid"

This would start the first command in the background. There would then be a one second delay before the second command is started. When the second command finishes, the first command is killed.

The PID of the most recently started background process (a process started by using & at the end of it) is available as "$!", and here we use that fact to keep track of the PIDs of the first command.

  • If I can also ask--will ctrl-c cause both processes to be reliably killed? Oct 29, 2018 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.