Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop npm script running in background automatically

Im using npm scripts, I have some of them that should run in parallel. I've got something like this:

...
scripts: {
   "a": "taskA &",
   "preb": "npm run a",
   "b": "taskB"
}
...

This is fine! But I'd like to kill automatically taskA running the background after taskB has finished.

How can I do that? Thanks!

like image 220
franleplant Avatar asked Jan 06 '15 22:01

franleplant


People also ask

How do I stop npm from running?

To stop a running npm process, press CTRL + C or close the shell window.

How do I start and stop npm?

Use the npm start to start a package. As of the [email protected], you can make use of custom arguments when executing scripts. This command is used to stop a package. This command will run the stop script that is provided by the package.

Do you have to run npm start every time?

Nope, Anything you change over code, build will rerun for that change.


2 Answers

Using the concurrently package looks like a less tricky & painful way. This avoids detached processes (&) alltogether. And promises to be more cross-plattform, too.

npm install concurrently --save

and then in package.json

"runBoth": "concurrently \"npm run taskA\" \"npm run taskB\"",

Tested (under Ubuntu 16.04, npm 5.6), also see here.

like image 134
Frank Nocke Avatar answered Oct 30 '22 06:10

Frank Nocke


The npm-run-all package may be what you're looking for:

$ npm install --save npm-run-all

Then in your package.json file:

"scripts": {
    "runA": "taskA",
    "runB": "taskB",
    "runBoth": "npm-run-all -p runA runB"
}

(-p runs them in parallel, use -s for sequential.)

like image 20
Alastair Maw Avatar answered Oct 30 '22 06:10

Alastair Maw