Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Killing a node process using a package.json stop script

I am trying to write a stop command for my nodeJS application which greps for a node process and kills it, so far I have:

{
  ...
  "scripts": {
    "start": "node src/main/webapp/index.js",
    "stop": "kill \"$(ps ux | grep node | grep -Eo '^\\s+[0-9]+' | tr -d '[[:space:]]')\""
  }
  ...
}

If I run ps ux I get

  PID    PPID    PGID     WINPID   TTY         UID    STIME COMMAND
12228    8428   12228      12960  cons0     197608 22:02:12 /usr/bin/ps
 4840       1    4840       4840  cons0     197608 22:01:46 /usr/bin/bash
13484   12292   12292       8024  cons0     197608 22:02:07 /c/Program Files/nodejs/node

If I run echo "$(ps ux | grep node | grep -Eo '^\s+[0-9]+' | tr -d '[[:space:]]')" then I get output 13484 so I know that bit works. However when I run npm stop I get the error:

npm ERR! Windows_NT 6.3.9600
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "stop"
npm ERR! node v0.12.4
npm ERR! npm  v2.10.1
npm ERR! code ELIFECYCLE
npm ERR! [email protected] stop: `kill "$(ps ux | grep node | grep -Eo '^\s+[0-9]+' | tr -d '[[:space:]]')"`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] stop script 'kill "$(ps ux | grep node | grep -Eo '^\s+[0-9]+' | tr -d '[[:space:]]')"'.
npm ERR! This is most likely a problem with the app package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     kill "$(ps ux | grep node | grep -Eo '^\s+[0-9]+' | tr -d '[[:space:]]')"
npm ERR! You can get their info via:
npm ERR!     npm owner ls app
npm ERR! There is likely additional logging output above.

Why is this breaking? Is there an easier way to do this?

like image 884
Eduardo Avatar asked Apr 23 '26 08:04

Eduardo


1 Answers

This seems to work:

"scripts": {
  "start": "node src/main/webapp/index.js & echo $! > .pid",
  "stop": "kill $(cat .pid)"
}

Maybe this is good enough for what you are trying to achieve but I guess there are better ways.

like image 95
Anatol - user3173842 Avatar answered Apr 25 '26 23:04

Anatol - user3173842



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!