0

I want to rerun the node server, whenever there's changes in file, but i want to use fswatch. i am using fswatch with the shell script like

nohup node server.js &

but i can't run the same script again since it will throw EADDRINUSE.

  • whats the best way to restart the node via script with the help of fswatch (or alternate, without any new installation) ?
  • Why node doesn't have something like node restart?

what i could think of is get pid from ps and kill it with script, but i guess there should be a better solution.

Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
  • 1
    I like forever, https://www.npmjs.com/package/forever. I realize it would probably count as new installation, but still worth looking into, might help others. – Ray Stantz Feb 19 '15 at 17:24
  • will ``nodemon server,js`` serve your purpose? – Piyas De Feb 19 '15 at 17:40
  • Your might want to look into node-supervisor: https://github.com/isaacs/node-supervisor – Ben Feb 19 '15 at 20:35

2 Answers2

0

I am able to do that with the help of fswatch.

fswatch -o mydir | xargs -n1 -I{}  ps | grep '[n]ode server.js$' | awk '{system("kill "$1)}'

or putting the same code in seperate shell file. and using it as

xargs -n1 './location-of-shell-file.sh'

when i run grep, that process is also included in ps, so to exclude that i had to use

grep '[n]ode server.js'
Dineshkumar
  • 4,165
  • 5
  • 29
  • 43
-2

EADDRINUSE comes because of you are already bind on the same port. Node.js has no build-in restart mechanics, so yes, you should use some bash scripts (or frameworks) for good restarting background app

Rax Wunter
  • 2,677
  • 3
  • 25
  • 32