node.js - How to run node js even on server restart -
i built nodejs project , runs smoothly. use forever
service running file in background if server restarted daemon won't started automatically , should started manually. want run daemon server rebooted
you add forever command in .bash_profile
every time server restart, command executed.
nano ~/.bash_profile forever start app.js # add command file, or whatever command using. source ~/.bash_profile # important, else changes not take effect
next time, on server restart, command run, hence creating daemon of node script.
note: maybe not best solution, 1 have got.
update
as @dlmeetei, suggested, can start nodejs app service can use features given linux service.
first create file in /etc/systemd/system
, like:
touch /etc/systemd/system/[your-app-name].service nano /etc/systemd/system/[your-app-name].service
then, add , edit following script according relevance.
[unit] description=node.js example server #requires=after=mysql.service # requires mysql service run first [service] execstart=/usr/local/bin/node /opt/nodeserver/server.js # required on systems # workingdirectory=/opt/nodeserver restart=always # restart service after 10 seconds if node service crashes restartsec=10 # output syslog standardoutput=syslog standarderror=syslog syslogidentifier=nodejs-example #user=<alternate user> #group=<alternate group> environment=node_env=production port=1337 [install] wantedby=multi-user.target
enable service, marks service starting on boot.
systemctl enable [your-app-name].service
manage service
systemctl start [your-app-name].service systemctl stop [your-app-name].service systemctl status [your-app-name].service # ensure app running systemctl restart [your-app-name].service
reference: https://www.axllent.org/docs/view/nodejs-service-with-systemd/
thanks @dlmeetei sharing link.
Comments
Post a Comment