Answer the question
In order to leave comments, you need to log in
Who implements autostart of node.js applications?
Good time of the day. Who implements the service for starting and restarting a node.js application?
Interested in such features as autorun, restart when it crashes or when the code changes. The simpler the better :)
Answer the question
In order to leave comments, you need to log in
This is very easy to do through systemd.
First, we create a service file for our node application of the following type:
# nodeapp.service
[Service]
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=nodeapp
User=nodeuser
Environment=NODE_ENV=production PORT=3000
WorkingDirectory=/path/to/nodeappdir
ExecStart=/usr/bin/node app.js
[Install]
WantedBy=multi-user.target
sudo cp nodeapp.service /etc/systemd/system/nodeapp.service # копируем его куда надо
sudo nano /etc/systemd/system/nodeapp.service # смотрим, [редактируем,] [сохраняем,] закрываем
sudo systemctl daemon-reload # перезагружаем список "демонов"
sudo systemctl start nodeapp # запустить сервис
sudo systemctl enable nodeapp # делаем авто запускаемым при старте системы
# другие действия:
sudo systemctl disable nodeapp # отключаем авто запуск
sudo systemctl stop nodeapp # остановить сервис
sudo systemctl restart nodeapp # перезагрузка
systemctl status nodeapp # посмотреть статус
journalctl -u nodeapp.service # глянуть логи
pm2
Advantages compared to forever,
it can autostart * nix systems, while using all the options that exist today (if system.d is available, it will be added to it, otherwise, depending on the OS, for example, in ubuntu without system.d, sit in init.d)
Done this is one command: pm2 startup
Able to monitor memory and processor load, which is convenient when there is no time to look for a memory leak, and the application should work, it also allows you to monitor all this in real time
Log rotation. Intercepts stdout and stderr, writes logs to a file, makes it possible to connect to the output of the application in real time
Can run applications using other interpreters, as well as just binaries Can run applications
on node in a cluster
Config with launch options in json
run through the module forever
In the end, everything will come down to a command in the console forever restart app.js
The list of features is available by forever -h
* so that the restart makes sense, do not forget to hang a watcher on the project. supervisor
Good luck!
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question