Create a Start-up Service for Node.js*

Documentation

Troubleshooting

000020082

07/14/2017

These steps show you how to create a system service for a Node.js* file (blink sample). You can then start the service at boot.

For this example, we create a system service called blinkjs. This service runs a blink sample on the Intel® Edison Kit for Arduino* during boot. You can find the JavaScript blink sample at GitHub.

To create the service file for Node.js, complete the following steps:

  1. Create and name the service file with a .service extension in the /etc/system/system/ directory:

    root@MyEdison:~# vi blinkjs.service /etc/systemd/system

  2. Put the following content inside the file and change it to meet the needs of your service (Do not include comments):

    [Unit]
    Description=Blink JavaScript
    //Brief description of your system service

    [Service]
    ExecStart=/usr/bin/node /blink.js
    //Commands with their arguments that execute when service starts

    Restart=always //Service restarted regardless of whether it exited cleanly or not, got terminated abnormally by a signal, or hit a timeout.

    Environment=NODE_PATH=/usr/lib/node_modules //Path to command's binary

    [Install]
    WantedBy=multi-user.target

  3. To start the service, use the systemctl start command:

    root@MyEdison:~# systemctl start blinkjs.service

    blinkjs.service: Blink JavaScript with Node.js

    Loaded: loaded (/etc/systemd/system/blinkjs.service; disabled)

    Active: active (running) since Wed 2015-08-05 17:44:45 UTC; 137ms ago

    Main PID: 400 (node)

    CGroup: /system.slice/blinkjs.service

    +-400 /usr/bin/node /home/root/blink.js

    Aug 05 17:44:45 EDISON1_WW25.5 systemd[1]: Started Blink JavaScript with Node.js.

  4. To check the status of the system service, use the systemctl status command:

    root@MyEdison:~# systemctl status blinkjs.service

  5. To enable service to start at boot time, use the systemctl enable command:

    root@MyEdison:~# systemctl enable blinkjs.service

  6. To restart the system service when changes are made to the service file, use the systemctl daemon-reload command:

    root@MyEdison:~# systemctl daemon-reload