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.
When running service controller start
from the command line it prompts for a username and password.
How can I run this service with proper permissions so it boots up with the Pi?
you need to allow the following commands in the sudoers file:
dont use the service command.
Remember to replace the service name with your systemd service. For example if your service file is with name:
then it should be:
also , you will need to do:
after you place you service file in systemd
Adding commonads in sudoers:
https://www.atrixnet.com/allow-an-unprivileged-user-to-run-a-certain-command-with-sudo/
example command: