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?
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.
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
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
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 <команда> &
ps -e -o pid,args --forest
Look for the
pid (Numbers) opposite your process, and enter (without
kill -TERM <pid вашего процесса>
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.
You can also use the
start-stop-daemon -Sbm -p /var/run/nodejs.pid -x node -- some.js
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:
And start your service:
systemctl enable [имя_сервиса].service systemctl start [имя_сервиса].service
As a bonus, watch the application logs:
journalctl -u [имя_сервиса]