node.jsmacosfswatch

Node Restart in Shell Script


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.

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


Solution

  • 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'