Starting node process with systemd

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> []

1 Like

Hmm,
my service looks like this.

[Unit]
Description=MATRIX OS Service
After=matrixio-malos.target

[Service]
User=pi
Group=pi
WorkingDirectory=/home/pi/Code/matrix-os

#Environment=MATRIX_MODE=service
Environment=MATRIX_DEVICE_ID=<id>
Environment=MATRIX_DEVICE_SECRET=<secret>
Environment=NODE_ENV=production

ExecStart=/usr/local/bin/node index.js
Restart=always
StandardOutput=syslog
SyslogIdentifier=matrix-node

[Install]
WantedBy=multi-user.target

But it I cannot set
Environment=MATRIX_MODE=service

Any ideas?

You are aware that the # sign in front of the line with the MATRIX_MODE variable is a comment sign?

Yes, I am aware of that.
But if I remove the #, the service keeps restarting again and again, seemingly due to access rights.

Both directory and files are all owned by the same user and group (pi:pi), which should match the service configuration.

Did you try to start the process manually as user “pi”, while setting the environment variables beforehand?

Whe I start the service without MATRIX_MODE=service as user pi, I get the follwoing:

==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
Authentication is required to stop 'matrixio-os.service'.
Multiple identities can be used for authentication:
 1.  ,,, (pi)
 2.  root
Choose identity to authenticate as (1-2): 1
Password:
==== AUTHENTICATION COMPLETE ===

Now I just started the service with MATRIX_MODE=service enabled as user pi.
The service started without problems and even installed and or updated the installed matrix-os apps.

So far, so good.

If I now start one of the apps from my phone, I can find the follwoing error in syslog:

 firebase config valid, but not validated, check flow
Apr  9 14:31:01 pixie matrix-node[5703]: ==== Application HelloWorld started! ====
Apr  9 14:31:02 pixie matrix-node[5703]: (HelloWorld)err module.js:457
Apr  9 14:31:02 pixie matrix-node[5703]:     throw err;
Apr  9 14:31:02 pixie matrix-node[5703]:     ^
Apr  9 14:31:02 pixie matrix-node[5703]: Error: Cannot find module './../matrix.js'
Apr  9 14:31:02 pixie matrix-node[5703]:     at Function.Module._resolveFilename (module.js:455:15)
Apr  9 14:31:02 pixie matrix-node[5703]:     at Function.Module._load (module.js:403:25)
Apr  9 14:31:02 pixie matrix-node[5703]:     at Module.require (module.js:483:17)
Apr  9 14:31:02 pixie matrix-node[5703]:     at require (internal/module.js:20:19)
Apr  9 14:31:02 pixie matrix-node[5703]:     at Object.<anonymous> (/var/matrix-os/store/apps/HelloWorld.matrix/index.js:3:10)
Apr  9 14:31:02 pixie matrix-node[5703]:     at Module._compile (module.js:556:32)
Apr  9 14:31:02 pixie matrix-node[5703]:     at Object.Module._extensions..js (module.js:565:10)
Apr  9 14:31:02 pixie matrix-node[5703]:     at Module.load (module.js:473:32)
Apr  9 14:31:02 pixie matrix-node[5703]:     at tryModuleLoad (module.js:432:12)
Apr  9 14:31:02 pixie matrix-node[5703]:     at Function.Module._load (module.js:424:3)
Apr  9 14:31:02 pixie matrix-node[5703]: MATRIX OS quits applications when they write to `stderr`. This is commonly triggered by the debug node module (run DEBUG=false to get by in dev mode) or with console.error(). If you find this behavior unacceptable, contact us, this "feature" is open to discussion.
Apr  9 14:31:02 pixie matrix-node[5703]: Stopping HelloWorld
Apr  9 14:31:02 pixie matrix-node[5703]: App IPC Disconnect HelloWorld
Apr  9 14:31:02 pixie matrix-node[5703]: Event Listeners Removed HelloWorld
Apr  9 14:31:02 pixie matrix-node[5703]: VES Instances yet not halted on app stop

Better than before, but still not working.

Any hints and tips appreciated.