How do I start and stop a systemctl service inside a bash script?












3















I am writing a bash script to perform a daily backup. This script will ultimately run automatically every morning (cron or systemd).



What I would like to accomplish is




  1. Stop myservice

  2. Perform backup procedures

  3. Start myservice


The bash script I have created looks something like this:



# Stop myservice
systemctl stop myservice.service

# Do all the backing up here...

# Start myservice
systemctl start myservice.service


The issue I am having is that when I run this script, it requires my password during the systemctl stop/start calls. If this is to run automatically, obviously it can't require a password every time. How do I run this script automatically without requiring this password?



Ubuntu 18.04



Thanks!










share|improve this question



























    3















    I am writing a bash script to perform a daily backup. This script will ultimately run automatically every morning (cron or systemd).



    What I would like to accomplish is




    1. Stop myservice

    2. Perform backup procedures

    3. Start myservice


    The bash script I have created looks something like this:



    # Stop myservice
    systemctl stop myservice.service

    # Do all the backing up here...

    # Start myservice
    systemctl start myservice.service


    The issue I am having is that when I run this script, it requires my password during the systemctl stop/start calls. If this is to run automatically, obviously it can't require a password every time. How do I run this script automatically without requiring this password?



    Ubuntu 18.04



    Thanks!










    share|improve this question

























      3












      3








      3








      I am writing a bash script to perform a daily backup. This script will ultimately run automatically every morning (cron or systemd).



      What I would like to accomplish is




      1. Stop myservice

      2. Perform backup procedures

      3. Start myservice


      The bash script I have created looks something like this:



      # Stop myservice
      systemctl stop myservice.service

      # Do all the backing up here...

      # Start myservice
      systemctl start myservice.service


      The issue I am having is that when I run this script, it requires my password during the systemctl stop/start calls. If this is to run automatically, obviously it can't require a password every time. How do I run this script automatically without requiring this password?



      Ubuntu 18.04



      Thanks!










      share|improve this question














      I am writing a bash script to perform a daily backup. This script will ultimately run automatically every morning (cron or systemd).



      What I would like to accomplish is




      1. Stop myservice

      2. Perform backup procedures

      3. Start myservice


      The bash script I have created looks something like this:



      # Stop myservice
      systemctl stop myservice.service

      # Do all the backing up here...

      # Start myservice
      systemctl start myservice.service


      The issue I am having is that when I run this script, it requires my password during the systemctl stop/start calls. If this is to run automatically, obviously it can't require a password every time. How do I run this script automatically without requiring this password?



      Ubuntu 18.04



      Thanks!







      command-line 18.04 bash backup






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 21 '18 at 16:41









      noslenkwahnoslenkwah

      1183




      1183






















          2 Answers
          2






          active

          oldest

          votes


















          5














          You have multiple possibilities, depending on your needs and preferences.



          An apparent approach …



          … would be to run the whole script as user root by adding it to root's crontab (using sudo crontab -e). It won't need any password then when systemctl stop/start myservice.service is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah) and have to switch to that other user for the backup. Example:



          # Stop myservice
          systemctl stop myservice.service

          # Do all the backing up here...
          # ... and run the backup_command as user "otheruser":
          sudo -u noslenkwah /path/to/backup_command --with --some --options

          # Start myservice
          systemctl start myservice.service


          Another approach …



          … would be to add the systemctl commands to a file in the /etc/sudoers.d directory so that a specific user may run them without supplying a password.




          1. issue sudo visudo -f /etc/sudoers.d/noslenkwah
            (The filename, noslenkwah doesn't matter, it is just a
            personal habit of mine to name the files after the "main"
            user affected by the settings in that file. It just needs
            to be a file below the directory /etc/sudoers.d.)



          2. Add the following lines and save the file.



            Cmnd_Alias MYSERVICE = 
            /bin/systemctl stop myservice.service,
            /bin/systemctl start myservice.service

            noslenkwah ALL = (root) NOPASSWD: MYSERVICE



          This allows the user noslenkwah to run sudo systemctl stop myservice.service and sudo systemctl start myservice.service without a password. It defines a socalled command alias (collection of commands) named MYSERVICE and then allows




          • the user noslenkwah

          • on ALL computers

          • as user root

          • without a password

          • to run the commands defined by MYSERVICE


          Replace noslenkwah and myservice with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service for this to work (not sudo systemctl start myservice (without .service, for example).



          Don't care about the "on ALL computers" part. This is relevant only if you intend to distribute the very same sudoers file to multiple computers.



          You would then change your backup script to



          # Stop myservice
          sudo systemctl stop myservice.service

          # Do all the backing up here...
          /path/to/backup_command --with --some --options

          # Start myservice
          sudo systemctl start myservice.service


          and have it run as user noslenkwah.






          share|improve this answer































            4














            Run this shell script as root.



            For example, put it in the root crontab:
            sudo crontab -e






            share|improve this answer























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "89"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1103632%2fhow-do-i-start-and-stop-a-systemctl-service-inside-a-bash-script%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              5














              You have multiple possibilities, depending on your needs and preferences.



              An apparent approach …



              … would be to run the whole script as user root by adding it to root's crontab (using sudo crontab -e). It won't need any password then when systemctl stop/start myservice.service is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah) and have to switch to that other user for the backup. Example:



              # Stop myservice
              systemctl stop myservice.service

              # Do all the backing up here...
              # ... and run the backup_command as user "otheruser":
              sudo -u noslenkwah /path/to/backup_command --with --some --options

              # Start myservice
              systemctl start myservice.service


              Another approach …



              … would be to add the systemctl commands to a file in the /etc/sudoers.d directory so that a specific user may run them without supplying a password.




              1. issue sudo visudo -f /etc/sudoers.d/noslenkwah
                (The filename, noslenkwah doesn't matter, it is just a
                personal habit of mine to name the files after the "main"
                user affected by the settings in that file. It just needs
                to be a file below the directory /etc/sudoers.d.)



              2. Add the following lines and save the file.



                Cmnd_Alias MYSERVICE = 
                /bin/systemctl stop myservice.service,
                /bin/systemctl start myservice.service

                noslenkwah ALL = (root) NOPASSWD: MYSERVICE



              This allows the user noslenkwah to run sudo systemctl stop myservice.service and sudo systemctl start myservice.service without a password. It defines a socalled command alias (collection of commands) named MYSERVICE and then allows




              • the user noslenkwah

              • on ALL computers

              • as user root

              • without a password

              • to run the commands defined by MYSERVICE


              Replace noslenkwah and myservice with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service for this to work (not sudo systemctl start myservice (without .service, for example).



              Don't care about the "on ALL computers" part. This is relevant only if you intend to distribute the very same sudoers file to multiple computers.



              You would then change your backup script to



              # Stop myservice
              sudo systemctl stop myservice.service

              # Do all the backing up here...
              /path/to/backup_command --with --some --options

              # Start myservice
              sudo systemctl start myservice.service


              and have it run as user noslenkwah.






              share|improve this answer




























                5














                You have multiple possibilities, depending on your needs and preferences.



                An apparent approach …



                … would be to run the whole script as user root by adding it to root's crontab (using sudo crontab -e). It won't need any password then when systemctl stop/start myservice.service is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah) and have to switch to that other user for the backup. Example:



                # Stop myservice
                systemctl stop myservice.service

                # Do all the backing up here...
                # ... and run the backup_command as user "otheruser":
                sudo -u noslenkwah /path/to/backup_command --with --some --options

                # Start myservice
                systemctl start myservice.service


                Another approach …



                … would be to add the systemctl commands to a file in the /etc/sudoers.d directory so that a specific user may run them without supplying a password.




                1. issue sudo visudo -f /etc/sudoers.d/noslenkwah
                  (The filename, noslenkwah doesn't matter, it is just a
                  personal habit of mine to name the files after the "main"
                  user affected by the settings in that file. It just needs
                  to be a file below the directory /etc/sudoers.d.)



                2. Add the following lines and save the file.



                  Cmnd_Alias MYSERVICE = 
                  /bin/systemctl stop myservice.service,
                  /bin/systemctl start myservice.service

                  noslenkwah ALL = (root) NOPASSWD: MYSERVICE



                This allows the user noslenkwah to run sudo systemctl stop myservice.service and sudo systemctl start myservice.service without a password. It defines a socalled command alias (collection of commands) named MYSERVICE and then allows




                • the user noslenkwah

                • on ALL computers

                • as user root

                • without a password

                • to run the commands defined by MYSERVICE


                Replace noslenkwah and myservice with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service for this to work (not sudo systemctl start myservice (without .service, for example).



                Don't care about the "on ALL computers" part. This is relevant only if you intend to distribute the very same sudoers file to multiple computers.



                You would then change your backup script to



                # Stop myservice
                sudo systemctl stop myservice.service

                # Do all the backing up here...
                /path/to/backup_command --with --some --options

                # Start myservice
                sudo systemctl start myservice.service


                and have it run as user noslenkwah.






                share|improve this answer


























                  5












                  5








                  5







                  You have multiple possibilities, depending on your needs and preferences.



                  An apparent approach …



                  … would be to run the whole script as user root by adding it to root's crontab (using sudo crontab -e). It won't need any password then when systemctl stop/start myservice.service is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah) and have to switch to that other user for the backup. Example:



                  # Stop myservice
                  systemctl stop myservice.service

                  # Do all the backing up here...
                  # ... and run the backup_command as user "otheruser":
                  sudo -u noslenkwah /path/to/backup_command --with --some --options

                  # Start myservice
                  systemctl start myservice.service


                  Another approach …



                  … would be to add the systemctl commands to a file in the /etc/sudoers.d directory so that a specific user may run them without supplying a password.




                  1. issue sudo visudo -f /etc/sudoers.d/noslenkwah
                    (The filename, noslenkwah doesn't matter, it is just a
                    personal habit of mine to name the files after the "main"
                    user affected by the settings in that file. It just needs
                    to be a file below the directory /etc/sudoers.d.)



                  2. Add the following lines and save the file.



                    Cmnd_Alias MYSERVICE = 
                    /bin/systemctl stop myservice.service,
                    /bin/systemctl start myservice.service

                    noslenkwah ALL = (root) NOPASSWD: MYSERVICE



                  This allows the user noslenkwah to run sudo systemctl stop myservice.service and sudo systemctl start myservice.service without a password. It defines a socalled command alias (collection of commands) named MYSERVICE and then allows




                  • the user noslenkwah

                  • on ALL computers

                  • as user root

                  • without a password

                  • to run the commands defined by MYSERVICE


                  Replace noslenkwah and myservice with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service for this to work (not sudo systemctl start myservice (without .service, for example).



                  Don't care about the "on ALL computers" part. This is relevant only if you intend to distribute the very same sudoers file to multiple computers.



                  You would then change your backup script to



                  # Stop myservice
                  sudo systemctl stop myservice.service

                  # Do all the backing up here...
                  /path/to/backup_command --with --some --options

                  # Start myservice
                  sudo systemctl start myservice.service


                  and have it run as user noslenkwah.






                  share|improve this answer













                  You have multiple possibilities, depending on your needs and preferences.



                  An apparent approach …



                  … would be to run the whole script as user root by adding it to root's crontab (using sudo crontab -e). It won't need any password then when systemctl stop/start myservice.service is run. The downside is that you may need to run the backup tasks as another user (say noslenkwah) and have to switch to that other user for the backup. Example:



                  # Stop myservice
                  systemctl stop myservice.service

                  # Do all the backing up here...
                  # ... and run the backup_command as user "otheruser":
                  sudo -u noslenkwah /path/to/backup_command --with --some --options

                  # Start myservice
                  systemctl start myservice.service


                  Another approach …



                  … would be to add the systemctl commands to a file in the /etc/sudoers.d directory so that a specific user may run them without supplying a password.




                  1. issue sudo visudo -f /etc/sudoers.d/noslenkwah
                    (The filename, noslenkwah doesn't matter, it is just a
                    personal habit of mine to name the files after the "main"
                    user affected by the settings in that file. It just needs
                    to be a file below the directory /etc/sudoers.d.)



                  2. Add the following lines and save the file.



                    Cmnd_Alias MYSERVICE = 
                    /bin/systemctl stop myservice.service,
                    /bin/systemctl start myservice.service

                    noslenkwah ALL = (root) NOPASSWD: MYSERVICE



                  This allows the user noslenkwah to run sudo systemctl stop myservice.service and sudo systemctl start myservice.service without a password. It defines a socalled command alias (collection of commands) named MYSERVICE and then allows




                  • the user noslenkwah

                  • on ALL computers

                  • as user root

                  • without a password

                  • to run the commands defined by MYSERVICE


                  Replace noslenkwah and myservice with the actual username and service name. Note that you really must issue sudo systemctl start myservice.service for this to work (not sudo systemctl start myservice (without .service, for example).



                  Don't care about the "on ALL computers" part. This is relevant only if you intend to distribute the very same sudoers file to multiple computers.



                  You would then change your backup script to



                  # Stop myservice
                  sudo systemctl stop myservice.service

                  # Do all the backing up here...
                  /path/to/backup_command --with --some --options

                  # Start myservice
                  sudo systemctl start myservice.service


                  and have it run as user noslenkwah.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 21 '18 at 19:18









                  PerlDuckPerlDuck

                  5,85111333




                  5,85111333

























                      4














                      Run this shell script as root.



                      For example, put it in the root crontab:
                      sudo crontab -e






                      share|improve this answer




























                        4














                        Run this shell script as root.



                        For example, put it in the root crontab:
                        sudo crontab -e






                        share|improve this answer


























                          4












                          4








                          4







                          Run this shell script as root.



                          For example, put it in the root crontab:
                          sudo crontab -e






                          share|improve this answer













                          Run this shell script as root.



                          For example, put it in the root crontab:
                          sudo crontab -e







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Dec 21 '18 at 16:47









                          EASYEASY

                          515




                          515






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Ask Ubuntu!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1103632%2fhow-do-i-start-and-stop-a-systemctl-service-inside-a-bash-script%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Plaza Victoria

                              Puebla de Zaragoza

                              Musa