Tuesday, March 15, 2022

Restarting Node on DO

If you shutdown your droplet (e.g. using “shutdown now”), then you’d need to bring it back up via the control panel. Doing a shutdown is like powering off. But if you restart (e.g. “reboot now”) then it’ll automatically come back up in a few minutes.

However, if all you are trying to do is stop your current Node process so that you can start a different one on the same port, then you don’t need to reboot your droplet at all. You can find the process id of the Node process and kill it from the command line. For example, if you started your process with

nohup node index.js & 

then:

ps -ef | grep node
will show something like:

root    9560  9526  13  06:30  pts/0   00:00:00  node index.js

The 2nd column is the process id. You can terminate the process with the kill command:

kill 9560
If it doesn’t die, try:

kill -9 9560
Another option is to not use nohup at all. You can use the PM2 process manager to keep your Node process running and also to restart or stop it. On your Droplet you’d do:

npm install pm2@latest -g

pm2 start index.js

Then:

pm2 ls
will show your running process.


pm2 restart index.js 
will restart it.


pm2 stop index.js
will stop the process.


pm2 delete index.js
if you no longer want pm2 to manage your process.

An advantage of pm2 over nohup is that if your Droplet reboots for some reason, pm2 will bring your Node process back up automatically.

Post a Comment

Note: Only a member of this blog may post a comment.