They gave us a trial week on VPS hosting, put NodeJS there, but the problem is that it works only when the console is activated. How to make it work endlessly?
6 answers
Three years have passed since the last reply. Node.js has essentially matured during this time, so the question has not lost its relevance.
I think it is worth mentioning a few more options that are not available in other answers.
Forever
There is a forever package in the Node.js ecosystem. It can be used as a spawning daemon for your application. One of the advantages of this solution is the simplicity and the ability not to write additional code. In addition, you can configure forever so that it automatically restarts the application when it crashes. Of the minuses - the need to drag into the node.js system a script daemon that needs to be installed globally. In some cases this may be unacceptable. Running the application using this solution looks like this:
forever start app.js
and his stop:
forever stop app.js
PM2
Another popular solution from the Node.js ecosystem is the PM2 process manager. As in the case with forever, pm2 is a global daemon process installed by node.js (with all the pluses and minuses). After it is installed, the application is launched using the command
pm2 start app.js
and his stop:
pm2 stop app.js
init.d
You can write a regular init.d script for your application. This path is used by all normal Linux services. If we talk about the advantages of this solution, then your application will start and stop the system itself, there is no need to install an extra daemon process. Of the minuses - you restart the application when you drop it manually (or with the help of special utilities). You will also need to write the init.d script itself. For example, you can take this decision as a basis. Running the application using this solution looks like this
service app-service-name start
and his stop
service app-service-name stop
Enter the command like this (without <>
):
nohup <ΠΊΠΎΠΌΠ°Π½Π΄Π°> &
Completion:
ps -e -o pid,args --forest
Look for the pid
(Numbers) opposite your process, and enter (without <>
):
kill -TERM <pid Π²Π°ΡΠ΅Π³ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠ°>
- 2@Zow, yes, everything is correct,
Node.js
launched in the background, therefore, it ignores input and all its output is saved in the filenohup.out
, in the directory withNode.js
- Niki-Timofe - 2@ niki-timofe, but now I understand everything. I just started from Windows via putty, from Ubunt everything is OK - Zow
- 2@ niki-timofe, and now how to disable this process? - Zow
- 2@Zow, wrote in response. - Niki-Timofe
- 2@ niki-timofe, and how to restart))) - Zow
All previous answers look like a temporary solution. To do everything thoroughly, you need to turn node.js into a full-blown daemon. For this, there is a set of daemontools utilities (not to be confused with the Windows utility for mounting CD images). Compared to the temporary options, the demon has at least one advantage. If, in your absence, node.js collapses, the special supervisor will immediately detect this and instantly re-raise it.
- one@Shamov after installation, you can work through nohup? - Zow
- @Zow possible, but not necessary. There are special commands to control the daemon. - Shamov
- oneAnother option is to write an init script in the following manner: https://gist.github.com/715255 . And to give the monitoring task, for example, to monit. - drdaeman
- @Shamov, and there are guidelines or examples of special commands, the current is not in English. If not, could you write how to start and stop the process? - Zow 2:57 pm
- one@Shamov, the admins have installed, but they also donβt rummage how to work with it. Tell me how to start and stop the process. hashcode.ru/questions/142176 - Zow
You can also use the start-stop-daemon
utility.
Run:
start-stop-daemon -Sbm -p /var/run/nodejs.pid -x node -- some.js
Stop:
start-stop-daemon -K -p /var/run/nodejs.pid
Run the screen command. This will open an already independent console from the ssh connection.
To go to the previously launched screen, you need to run screen -r
Another option is to use systemd if it is on your system. Create a file /usr/lib/systemd/system/[time_service_service Next, we put the config file into it:
[Service] ExecStart=[node binary] [main file] Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=node-sample User=srv-node-sample Group=srv-node-sample Environment=NODE_ENV=production [Install] WantedBy=multi-user.target
After that, make a symlink on it:
ln -s /usr/lib/systemd/system/[ΠΈΠΌΡ_ΡΠ΅ΡΠ²ΠΈΡΠ°].service /etc/systemd/system/multi-user.target.wants/[ΠΈΠΌΡ_ΡΠ΅ΡΠ²ΠΈΡΠ°].service`
Reboot the systemctl:
systemctl daemon-reload`
And start your service:
systemctl enable [ΠΈΠΌΡ_ΡΠ΅ΡΠ²ΠΈΡΠ°].service systemctl start [ΠΈΠΌΡ_ΡΠ΅ΡΠ²ΠΈΡΠ°].service
As a bonus, watch the application logs:
journalctl -u [ΠΈΠΌΡ_ΡΠ΅ΡΠ²ΠΈΡΠ°]