Starting node process with systemd


#1

Hi,

if you would like to get the “node index.js” started by system start of your RPi 3 proceed like this:

  1. Create a user “matrix” with group “matrix”

  2. Install all software as described here: https://matrix-io.github.io/matrix-documentation/matrix-os/getting-started/installation/ (for the RPi related part do this with your newly created “matrix” user). If installation fails on your RPi with Raspian Stretch, have a look in this post: SSH into RPi3 - MALOS Unavailable

  3. Create a file /etc/systemd/system/matrix-node.service (replace “<DEVICE ID>” and “<DEVICE SECRET>” with the values you got when registering your device):

    [Unit]
    Description=Matrix NodeJS Service
    After=network.target

    [Service]
    ExecStart=/usr/local/bin/node index.js
    Restart=always
    StandardOutput=syslog
    SyslogIdentifier=matrix-node
    User=matrix
    Group=matrix
    Environment=NODE_ENV=production
    Environment=MATRIX_DEVICE_ID=<DEVICE ID>
    Environment=MATRIX_DEVICE_SECRET=<DEVICE SECRET>
    WorkingDirectory=/home/matrix/matrix-os

    [Install]
    WantedBy=multi-user.target

  4. Enable the new Service:

    systemctl daemon-reload
    systemctl enable matrix-node

  5. Reboot your RPi

  6. After rebooting a “systemctl status matrix-node” should produce an output like this:

    root@matrix:~# systemctl status matrix-node.service
    ● matrix-node.service - Matrix NodeJS Service
    Loaded: loaded (/etc/systemd/system/matrix-node.service; enabled; vendor preset: enabled)
    Active: active (running) since Sun 2017-10-15 10:21:38 UTC; 12min ago
    Main PID: 3322 (node)
    CGroup: /system.slice/matrix-node.service
    └─3322 /usr/local/bin/node index.js

    Okt 15 10:26:00 matrix matrix-node[3322]: hb: sen> [] svc> []