10

i need to run "node js" project on 16.4 Permanently

and use forever package for run in background in ubuntu

now I want add a startup service to ubuntu but i searched there is no result.

I've created a file called test.conf to /etc/init.d

test.conf :

start on startup
exec forever start /root/node/node_modules/.bin/www
  • You will need to use a systemd service for this. – George Udosen Mar 19 '17 at 18:09
  • Will you be using mysql – George Udosen Mar 19 '17 at 18:36
  • @George There are training in this topic? – Hesam Pourghazian Mar 19 '17 at 19:01
  • What topic are you referring to, have you set up forever and just need a way to run it in Ubuntu service? – George Udosen Mar 19 '17 at 19:04
  • @George yes ... – Hesam Pourghazian Mar 20 '17 at 5:22
13

In the simplest for using systemd service:

  1. Install forever:

    [sudo] npm install forever -g
    
  2. Write and store the script to run in preferred location.

  3. Write the Systemd service:

    [Unit]
    Description=forever service
    After=network.target
    
    
    [Service]
    ExecStart=/home/george/.npm-global/bin/forever start /root/node/node_modules/.bin/www
    ExecStop=/home/george/.npm-global/bin/forever stop /root/node/node_modules/.bin/www
    Restart=always
    RestartSec=10                       # Restart service after 10 seconds if node service crashes
    StandardOutput=syslog               # Output to syslog
    StandardError=syslog                # Output to syslog
    SyslogIdentifier=nodejs-example
    
    
    [Install]
    WantedBy=multi-user.target
    
  4. Save the systemd service file in /etc/systemd/system as myforever.service ( or with whatever name you like ).

  5. Start the service and enable at start up.

    sudo systemctl start myforever.service
    sudo systemctl enable myforever.service
    
  6. Check if it's running:

    sudo systemctl status myforever.service
    
  7. To stop and disable it any time:

    sudo systemctl stop myforever.service
    sudo systemctl disable myforever.service
    

NOTE:

  1. This is a simplified version of a systemd service many options are available
  2. The service can also be called myforever without the .service extension, systemd will pick the right file
  3. This /home/george/.npm-global/bin/forever is where my node modules are kept, yours will be different. Find it with which forever

Additional Information:

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

  • finally i use package "service-systemd" – Hesam Pourghazian Mar 25 '17 at 20:01
  • @david6 yes it is, thanks will update – George Udosen Apr 23 '17 at 12:17
0

i use "forever service-systemd " because my ubuntu is 16.04

first use : package --> forever and check this pages :

if upstart : https://github.com/zapty/forever-service

if systemd : https://www.npmjs.com/package/service-systemd

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.