Restart service from bash script

5,531

Your script restart.sh use the systemctl command, which is part of systemd; because this command needs root rights due to his role, you need to use the sudo command to be able to use it (and your user must be refered in sudoers as an authorized sudo user). Your command would be like below :

sudo /home/myuser/restart.sh
  • /home/myuser/restart.sh is just the absolute path to start the script, which is the same thing as doing sudo ./restart.sh inside /home/myuser/ folder

Also, as OP mentionned it in comment, you can also call sudo with absolute path with /usr/bin/sudo (/usr/bin/sudo apt update is the same as sudo apt update)

Share:
5,531

Related videos on Youtube

jidexl21
Author by

jidexl21

Updated on September 18, 2022

Comments

  • jidexl21
    jidexl21 almost 2 years

    I'm trying to restart a service myservice from a service that is run as a daemon.

    I've created a file called myapprestart in /etc/sudoers.d/ that looks like this

    %myuser ALL=NOPASSWD: /bin/systemctl restart myservice
    %myuser ALL=NOPASSWD: /bin/systemctl start myservice
    %myuser ALL=NOPASSWD: /bin/systemctl stop myservice
    

    The myapprestart.service daemon is running successfully as a timed unit that calls /home/myuser/restart.sh but always fails with the following line in journalctl

    failed to stop myservice.service: Interactive authentication required.
    

    the script file/home/myuser/restart.sh contains lines like below

    systemctl stop myservice
    systemctl start myservice
    

    I need some pointers on what i'm not doing right. I've looked at at least four similar questions, but for some reason this case seems not to work. I'm using ubuntu 19.02.

    • damadam
      damadam over 4 years
      you need to run your script with sudo permission, like that : sudo /home/myuser/restart.sh
    • Eliah Kagan
      Eliah Kagan over 4 years
      @damadam I'm pretty sure that's the answer--would you be willing to post it (or something like it) as one?
    • damadam
      damadam over 4 years
      @EliahKagan depends if OP want something that could be automatic or not
    • user535733
      user535733 over 4 years
      If you need to restart a service that has stopped or crashed, use the proper systemd tools. The 'restart' stanza in the .service file, or a systemd timer, etc.
    • jidexl21
      jidexl21 over 4 years
      @damdam can I use sudo in systemd?
    • ajgringo619
      ajgringo619 over 4 years
      Does your service need to be run by root? If not, you could run it as a user service, forgoing the need for sudo.
    • jidexl21
      jidexl21 over 4 years
      I finally solved the problem, the summary is that i needed to call the command with sudo, so i had to update my ExecStart to /usr/bin/sudo /home/myuser/restart.sh
    • damadam
      damadam over 4 years
      @jidexl21 you ping me wrong, so I didn't see your comment, my username is damadam ;)
  • jidexl21
    jidexl21 over 4 years
    I also noted that even in a case where the command is whitelisted with nopass in the sudoers.d if called from a variable in the script like systemctl stop $unit where unit is myservice it fails