Start Service with Sudo

24,918

you need to allow the following commands in the sudoers file:

systemctl start <your-systemd-service>

systemctl stop <your-systemd-service>

systemctl restart <your-systemd-service>

systemctl enable <your-systemd-service>

dont use the service command.

Remember to replace the service name with your systemd service. For example if your service file is with name:

/etc/systemd/system/controller.service

then it should be:

systemctl start controller.service

systemctl stop controller.service

systemctl restart controller.service

systemctl enable controller.service

also , you will need to do:

systemctl daemon-reload

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:

<your-user> ALL=(ALL) NOPASSWD: /usr/bin/systemctl start controller.service
Share:
24,918

Related videos on Youtube

Matthew
Author by

Matthew

Make things. Make things fast.

Updated on September 18, 2022

Comments

  • Matthew
    Matthew over 1 year

    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?

    • George Udosen
      George Udosen over 5 years
      Your script is the issue!
    • Matthew
      Matthew over 5 years
      I see... How do I give the proper permissions to the script?
    • George Udosen
      George Udosen over 5 years
      No, the "asking for password" is the issue and I assume controller is your script!
    • Matthew
      Matthew over 5 years
      server.js is my script that calls /home/pi/Documents/node_modules/rpio/lib/rpio.js
    • Matthew
      Matthew over 5 years
      I'm not sure what you mean by "no"...
  • Matthew
    Matthew over 5 years
    Where is the sudoers file?
  • Ijaz Ahmad
    Ijaz Ahmad over 5 years
    just type visudo as root , it will open the config file and u need to allow your user thoese commands
  • Ijaz Ahmad
    Ijaz Ahmad over 5 years
    remember to replace the service name with the name of your systemd service
  • Matthew
    Matthew over 5 years
    I get an error after I save the file, >>> /etc/sudoers: syntax error near line 15 and so on for each line I try to add.
  • Matthew
    Matthew over 5 years
    How do I edit as root when I'm logged in as another user?
  • Ijaz Ahmad
    Ijaz Ahmad over 5 years
    you need to check the syntax of how to add entries in sudoers file
  • Ijaz Ahmad
    Ijaz Ahmad over 5 years
    you need to be root to edit the sudoers file , or ask your system admin
  • Ijaz Ahmad
    Ijaz Ahmad over 5 years
    allowing commands in sudeors file doesnt mean that you just copy paste them , they have specifc syntax , i have just pointed out the commands u need to allow , how to allow them is a different question
  • Matthew
    Matthew over 5 years
    I can run sudo visudo... Is that good enough?
  • Ijaz Ahmad
    Ijaz Ahmad over 5 years
  • Matthew
    Matthew over 5 years
    Even when running as root with su root then visudo I get a syntax error.
  • Matthew
    Matthew over 5 years
    Hm. Maybe it's slightly different on Raspbian?