3

I am trying to run an Express API server with sudo permissions and have it automatically start on boot.

The API needs access to bcm2835 library, and I get the following when trying to run node server.js

bcm2835_init: Unable to open /dev/mem: Permission denied /home/pi/Documents/node_modules/rpio/lib/rpio.js:104 return bindfunc(optarg);

Error: Could not initialize bcm2835 library
at bindcall (/home/pi/...) etc.

However it boots up fine running sudo node server.js.

I have created a service file at /etc/systemd/system as follows:

[Unit]
Description=Node API
After=network.target

[Service]
ExecStart=/usr/bin/node /home/pi/Documents/server.js
Restart=always
SyslogIdentifier=controller
User=root
Group=root
Environment=

TimeoutStopSec=30

[Install]
WantedBy=multi-user.target

I found some guidance here and here, but they seem slightly off, since a sub-command needs sudo/root privileges.

How can I run this service with proper permissions so it boots up with the Pi?

I think the boot is failing silently in the background.

10
  • You probably need some environment variables for node to run and find the library.
    – RalfFriedl
    Nov 1, 2018 at 14:18
  • Do you have an example? I'm not very familiar with environment variables, and I don't understand how a variable could be equivalent to running as sudo.
    – Matthew
    Nov 1, 2018 at 14:21
  • It's not related tosudo. Type env and see what might be used by node.
    – RalfFriedl
    Nov 1, 2018 at 15:00
  • Type env where?
    – Matthew
    Nov 1, 2018 at 15:56
  • 1
    At a shell prompt.
    – RalfFriedl
    Nov 1, 2018 at 15:57

1 Answer 1

1

/dev/mem must be run as root, so after a fresh install of Raspbian.

  1. Create the service

  2. Enable the service with systemctl enable nameOfService

0

Your Answer

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

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