If you’re using nodemon
to automatically restart your node/coffee
server process when the source code changes (and if you’re not, why
not?), you may find (as I did) that it can kill the script quite
abruptly. If you want to do something when the code shuts down (close
connections, flush to database/disk/redis/memcached, etc) then you can
do so by intercepting the SIGUSR2
signal that nodemon
uses to kill
your script, and then setting up these actions. Once these actions are
complete, you can exit but nodemon
will see that as a crash and stop
restarting. So you then have to kill your app again with SIGUSR2
.
Here’s the solution I use in CoffeeScript (click Toggle for JavaScript):
1 2 3 4 5 6 7 8 9 10 11 12 |
|
By the way, to install nodemon globally: sudo npm install -g nodemon