I am using PuTTy to run a node.js server on a shared host (cPanel). My question is whether it is possible to maintain a command even after I completely close PuTTy? My issue is that when I for example type ‘nohup node server.js &’ and my server runs successfully, the moment I close down PuTTy, my node server goes down again. I would love it if there was a way to continously have my node server running even after exiting PuTTy completely or even shutting down my computer.
I have tried the following things:
– nohup (as seen above), however that does not maintain the connection after I close down PuTTy.
– forever.js however still same issue as nohup.
Appreciate all the help I can get.
Edit: Just to clarify, my issue is not that I want the process to run in the background. My issue is whether it is possible to run the process even while my computer is shut down. It seems weird that I have to keep my PC on in order for the server process to continue, even though I’m using hosting.
4
Answers
Found out the issue was with cPanel, as it did not support node.js. I moved to DigitalOcean and it worked fantastically there.
There are multiple ways to keep the command running in background even if you close the putty terminal.
Very simple one is that you put “&” at the end of command like following
node server.js &
Second method is to use pm2. This is very efficient way to run the NodeJS server. It not only maintain session after closing terminal, but also if your operating system restarts, then it will automatically start the server again without user interaction to run the command.
You can install pm2 by following command
npm install pm2 -g
And then start the node server as
pm2 start server.js
Please update if this worked for you.
You can use screen if it is available on the server.
Probably you are looking for
nohup
ordisown
if need to run a process that won’t be terminated after closing the terminal:Or using
disown
:If you have multiple commands:
By typing jobs something like this appears:
To disown let’s say only job 2:
If your process is already running, you can pause it
ctrl+z
, something like this will appear:Then type bg to put it in background:
After that you can disown and exit: