3

I built a Nodejs project and now it runs smoothly. I use forever service for running file in background but if server get restarted the daemon won't be started automatically and should be started manually. I want to run the daemon even the server get rebooted

4
  • what OS are you using for your server? Commented Aug 20, 2017 at 9:35
  • @RaghavGarg I am using centos7 Commented Aug 20, 2017 at 9:41
  • Have you tried forever, pm2, nodemon, ...? Commented Aug 20, 2017 at 9:47
  • 1
    You should integrate with systemd, it has many advantages. Commented Aug 20, 2017 at 11:45

1 Answer 1

7

You could add the forever command in .bash_profile so that every time the server restart, your command will simply be also executed.

nano ~/.bash_profile
forever start app.js # add this command to the file, or whatever command you are using.
source ~/.bash_profile # very important, else changes will not take effect

Next time, on your server restart, your command will also run, hence creating a daemon of your node script.

Note: This is maybe not the best solution, but the one I have got.

Update

As @dlmeetei, suggested, you can also start your nodejs app like a service so that we can use the features given by a linux service.

First create a file in /etc/systemd/system, like:

touch /etc/systemd/system/[your-app-name].service
nano /etc/systemd/system/[your-app-name].service

Then, add and edit the following script according to your relevance.

[Unit]
Description=Node.js Example Server
#Requires=After=mysql.service # Requires the mysql service to run first

[Service]
ExecStart=/usr/local/bin/node /opt/nodeserver/server.js
# Required on some systems
# WorkingDirectory=/opt/nodeserver
Restart=always
# Restart service after 10 seconds if node service crashes
RestartSec=10
# Output to 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 the service, it will marks the service for starting up on boot.

systemctl enable [your-app-name].service

Manage the service

systemctl start [your-app-name].service
systemctl stop [your-app-name].service
systemctl status [your-app-name].service # ensure your app is running
systemctl restart [your-app-name].service

Reference: https://www.axllent.org/docs/view/nodejs-service-with-systemd/

Thanks @dlmeetei for sharing the link.

Sign up to request clarification or add additional context in comments.

4 Comments

This will only start your server when you start bash. Better to integrate with systemd or write init.d script based on lsb.
@dlmeetei, I am not too good in Unix system. are you suggesting to make a service of it? If not, can you please share, how it can be done with systemd or init.d, whichever is better?
@dlmeetei, this seems like a nice approach, I haven't really thought of starting a node app like a service. Thanks for sharing the link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.