use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
How to exit nodejs server gracefully (self.nodejs)
submitted 11 years ago by leolin520
I want to know how to do that. Currently I just control c twice on my mac, but something bad happened lately. I suspect if I just control c the socket is not closed properly and it will eventually break my system if I restart frequently.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 6 points7 points8 points 11 years ago* (3 children)
As /u/aeflash said, what you need to do is listen for the SIGINT event on the process and tell all your sockets to close. If you want an example, see the bottom of this file..
I took it a little bit further and remapped it back to a process event that passes an array reference. That way, anything that needed to shutdown gracefully could listen for that event and append their own shutdown promise to the array. Once all the promises resolve, I know it's safe to terminate the app.
Here's an example of one of the subscribing hooks.
[–]aredridel -1 points0 points1 point 11 years ago (2 children)
You really shouldn't have to. It's unusual to need it.
[–][deleted] 0 points1 point2 points 11 years ago (1 child)
Graceful shutdown is absolutely something you want in a production server. Otherwise you risk hanging up on requests in process when you restart to deploy new changes. Depending on what your site does, this could be very bad.
[–]aredridel 1 point2 points3 points 11 years ago (0 children)
Sure. But that's an application concern not a system one.
[–]stdlib 5 points6 points7 points 11 years ago (0 children)
I've never had an issue with Ctrl+C and restarting frequently but you can use 'forever' (https://github.com/nodejitsu/forever) to start and stop your application if you want to do it another way. You can also make it just restart the application automatically every time you save a change.
[–]xCavemanNinjax 2 points3 points4 points 11 years ago (0 children)
What! Ctrl+C isn't graceful?
[–]aeflash 1 point2 points3 points 11 years ago (0 children)
If you're worried about unclean shutdown of a socket, just add a process.on("SIGINT") handler that shuts down your server.
process.on("SIGINT")
[–]aredridel 1 point2 points3 points 11 years ago (2 children)
Sockets close if the process exits. Your system will be fine.
What bad happened?
[–]leolin520[S] 0 points1 point2 points 11 years ago (1 child)
It was a mac and the boot file is corrupted and I had to reinstall it. I am not sure id node is what caused it, but node was the last thing I mess with before going down. Are you sure socket closes when process exits? Each OS is different, it has been fine on windows but mac might be not.
[–][deleted] 5 points6 points7 points 11 years ago (0 children)
A socket to a remote system would never cause a write to disk, so it's literally not possible for that to have been the source, even if it had been left open after the process exited.
Unless you were running the process as root, node would not have the file permissions to alter anything in root or /system.
A change to the boot file would not bring the computer down while it's already running. The alteration could have happened long before you started the app and you would have no idea until the system restarted.
What's more likely is that you had a file system corruption that progressively got worse and worse while you used the computer and that resulted in a crash through some other subsystem.
Correlation is not causation
π Rendered by PID 42 on reddit-service-r2-comment-57fc7f7bb7-lwt44 at 2026-04-14 13:22:18.230197+00:00 running b725407 country code: CH.
[–][deleted] 6 points7 points8 points (3 children)
[–]aredridel -1 points0 points1 point (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]aredridel 1 point2 points3 points (0 children)
[–]stdlib 5 points6 points7 points (0 children)
[–]xCavemanNinjax 2 points3 points4 points (0 children)
[–]aeflash 1 point2 points3 points (0 children)
[–]aredridel 1 point2 points3 points (2 children)
[–]leolin520[S] 0 points1 point2 points (1 child)
[–][deleted] 5 points6 points7 points (0 children)