Could not resolve host: content.dropboxapi.com when to upload file at shutdown

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
3
down vote

favorite












I want to upload a file to my dropbox when my pc is shutting down.



sudo vim /etc/systemd/system/upload.service
[Unit]
Description=upload files into dropbox
Before=network.target shutdown.target reboot.target
Requires=network-online.target

[Service]
ExecStop=/bin/true
ExecStart=/bin/bash /home/upload.sh
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target


And the upload.sh script.



cd /home
curl -X POST https://content.dropboxapi.com/2/files/upload
--header "Authorization: Bearer xxxx"
--header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
--header "Content-Type: application/octet-stream"
--data-binary @"test.txt"


bash upload.sh can executed successfully,and test.txt file uploaded into my dropbox.



sudo systemctl enable upload service 


To reboot my pc.



sudo journalctl -u upload 

Apr 13 23:58:52 localhost systemd[1]: Started upload files into dropbox.
Apr 13 23:58:52 localhost systemd[1]: Starting upload files into dropbox...
Apr 13 23:58:52 localhost bash[117]: % Total % Received % Xferd Average Speed Time Time Time Current
Apr 13 23:58:52 localhost bash[117]: Dload Upload Total Spent Left Speed
Apr 13 23:58:52 localhost bash[117]: 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: content.dropboxapi.com; Un
Apr 13 23:58:52 localhost systemd[1]: upload.service: main process exited, code=exited, status=6/NOTCONFIGURED
Apr 13 23:58:52 localhost systemd[1]: Unit upload.service entered failed state.
Apr 13 23:58:52 localhost systemd[1]: upload.service failed.


Some DNS error Could not resolve host: content.dropboxapi.com result in upload.service failed.



I have added Requires=network-online.target in upload.service,how to make DNS parser resolve the host at my pc's shutdowning time?










share|improve this question



























    up vote
    3
    down vote

    favorite












    I want to upload a file to my dropbox when my pc is shutting down.



    sudo vim /etc/systemd/system/upload.service
    [Unit]
    Description=upload files into dropbox
    Before=network.target shutdown.target reboot.target
    Requires=network-online.target

    [Service]
    ExecStop=/bin/true
    ExecStart=/bin/bash /home/upload.sh
    StandardOutput=journal
    StandardError=journal

    [Install]
    WantedBy=multi-user.target


    And the upload.sh script.



    cd /home
    curl -X POST https://content.dropboxapi.com/2/files/upload
    --header "Authorization: Bearer xxxx"
    --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
    --header "Content-Type: application/octet-stream"
    --data-binary @"test.txt"


    bash upload.sh can executed successfully,and test.txt file uploaded into my dropbox.



    sudo systemctl enable upload service 


    To reboot my pc.



    sudo journalctl -u upload 

    Apr 13 23:58:52 localhost systemd[1]: Started upload files into dropbox.
    Apr 13 23:58:52 localhost systemd[1]: Starting upload files into dropbox...
    Apr 13 23:58:52 localhost bash[117]: % Total % Received % Xferd Average Speed Time Time Time Current
    Apr 13 23:58:52 localhost bash[117]: Dload Upload Total Spent Left Speed
    Apr 13 23:58:52 localhost bash[117]: 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: content.dropboxapi.com; Un
    Apr 13 23:58:52 localhost systemd[1]: upload.service: main process exited, code=exited, status=6/NOTCONFIGURED
    Apr 13 23:58:52 localhost systemd[1]: Unit upload.service entered failed state.
    Apr 13 23:58:52 localhost systemd[1]: upload.service failed.


    Some DNS error Could not resolve host: content.dropboxapi.com result in upload.service failed.



    I have added Requires=network-online.target in upload.service,how to make DNS parser resolve the host at my pc's shutdowning time?










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I want to upload a file to my dropbox when my pc is shutting down.



      sudo vim /etc/systemd/system/upload.service
      [Unit]
      Description=upload files into dropbox
      Before=network.target shutdown.target reboot.target
      Requires=network-online.target

      [Service]
      ExecStop=/bin/true
      ExecStart=/bin/bash /home/upload.sh
      StandardOutput=journal
      StandardError=journal

      [Install]
      WantedBy=multi-user.target


      And the upload.sh script.



      cd /home
      curl -X POST https://content.dropboxapi.com/2/files/upload
      --header "Authorization: Bearer xxxx"
      --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
      --header "Content-Type: application/octet-stream"
      --data-binary @"test.txt"


      bash upload.sh can executed successfully,and test.txt file uploaded into my dropbox.



      sudo systemctl enable upload service 


      To reboot my pc.



      sudo journalctl -u upload 

      Apr 13 23:58:52 localhost systemd[1]: Started upload files into dropbox.
      Apr 13 23:58:52 localhost systemd[1]: Starting upload files into dropbox...
      Apr 13 23:58:52 localhost bash[117]: % Total % Received % Xferd Average Speed Time Time Time Current
      Apr 13 23:58:52 localhost bash[117]: Dload Upload Total Spent Left Speed
      Apr 13 23:58:52 localhost bash[117]: 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: content.dropboxapi.com; Un
      Apr 13 23:58:52 localhost systemd[1]: upload.service: main process exited, code=exited, status=6/NOTCONFIGURED
      Apr 13 23:58:52 localhost systemd[1]: Unit upload.service entered failed state.
      Apr 13 23:58:52 localhost systemd[1]: upload.service failed.


      Some DNS error Could not resolve host: content.dropboxapi.com result in upload.service failed.



      I have added Requires=network-online.target in upload.service,how to make DNS parser resolve the host at my pc's shutdowning time?










      share|improve this question















      I want to upload a file to my dropbox when my pc is shutting down.



      sudo vim /etc/systemd/system/upload.service
      [Unit]
      Description=upload files into dropbox
      Before=network.target shutdown.target reboot.target
      Requires=network-online.target

      [Service]
      ExecStop=/bin/true
      ExecStart=/bin/bash /home/upload.sh
      StandardOutput=journal
      StandardError=journal

      [Install]
      WantedBy=multi-user.target


      And the upload.sh script.



      cd /home
      curl -X POST https://content.dropboxapi.com/2/files/upload
      --header "Authorization: Bearer xxxx"
      --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
      --header "Content-Type: application/octet-stream"
      --data-binary @"test.txt"


      bash upload.sh can executed successfully,and test.txt file uploaded into my dropbox.



      sudo systemctl enable upload service 


      To reboot my pc.



      sudo journalctl -u upload 

      Apr 13 23:58:52 localhost systemd[1]: Started upload files into dropbox.
      Apr 13 23:58:52 localhost systemd[1]: Starting upload files into dropbox...
      Apr 13 23:58:52 localhost bash[117]: % Total % Received % Xferd Average Speed Time Time Time Current
      Apr 13 23:58:52 localhost bash[117]: Dload Upload Total Spent Left Speed
      Apr 13 23:58:52 localhost bash[117]: 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0curl: (6) Could not resolve host: content.dropboxapi.com; Un
      Apr 13 23:58:52 localhost systemd[1]: upload.service: main process exited, code=exited, status=6/NOTCONFIGURED
      Apr 13 23:58:52 localhost systemd[1]: Unit upload.service entered failed state.
      Apr 13 23:58:52 localhost systemd[1]: upload.service failed.


      Some DNS error Could not resolve host: content.dropboxapi.com result in upload.service failed.



      I have added Requires=network-online.target in upload.service,how to make DNS parser resolve the host at my pc's shutdowning time?







      shutdown systemd dropbox






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 20 at 11:59









      goedt

      527




      527










      asked Apr 14 at 7:08









      it_is_a_literature

      50425




      50425




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted
          +150










          method 1: /etc/systemd/system/upload.service

          1.1 /etc/systemd/system/upload.service



          sudo vim /etc/systemd/system/upload.service
          [Unit]
          Description=upload files into dropbox
          Before=shutdown.target reboot.target
          Requires=network-online.target
          After=network.target

          [Service]
          ExecStop=/bin/true
          ExecStart=/bin/bash /home/upload.sh
          StandardOutput=journal
          StandardError=journal

          [Install]
          WantedBy=multi-user.target


          1.2 vim /home/upload.sh



          cd /home
          curl -X POST https://content.dropboxapi.com/2/files/upload
          --header "Authorization: Bearer xxxx"
          --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
          --header "Content-Type: application/octet-stream"
          --data-binary @"test.txt"


          1.3 sudo systemctl enable upload service



          method 2: /lib/systemd/system-shutdown/upload.service

          2.1 /lib/systemd/system-shutdown/upload.service



          sudo vim /lib/systemd/system-shutdown/upload.service
          [Unit]
          Description=upload files into dropbox
          Before=shutdown.target reboot.target
          Requires=network-online.target
          After=network.target

          [Service]
          ExecStop=/bin/true
          ExecStart=/bin/bash /home/upload.sh
          StandardOutput=journal
          StandardError=journal

          [Install]
          WantedBy=multi-user.target


          2.2 vim /home/upload.sh



          cd /home
          curl -X POST https://content.dropboxapi.com/2/files/upload
          --header "Authorization: Bearer xxxx"
          --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
          --header "Content-Type: application/octet-stream"
          --data-binary @"test.txt"


          2.3 sudo systemctl enable /lib/systemd/system-shutdown/upload.service






          share|improve this answer





























            up vote
            0
            down vote













            The best way to run a program or script while the system is going down is by using the ExecStop= of the unit to run such program. The ExecStart= can either be a dummy script (such as /bin/true) or, in recent versions of systemd, it can even be omitted.



            Regarding dependencies, you want to place your unit after the services you need, such as network-online.target and perhaps systemd-resolved.service if you're using it for DNS resolution. The reason for that is that systems are stopped at shutdown time in the reverse order in which they are started. So by ordering your service after the dependencies, it means your ExecStop= script will be run before the dependencies are stopped.



            Something like this should work:



            [Unit]
            Description=upload files into dropbox
            After=network.target systemd-resolved.service
            Requires=network-online.target

            [Service]
            Type=oneshot
            RemainAfterExit=yes
            ExecStart=/bin/true
            ExecStop=/bin/bash /home/upload.sh
            StandardOutput=journal
            StandardError=journal

            [Install]
            WantedBy=multi-user.target


            Enable the unit as usual (the --now argument also starts it):



            $ sudo systemctl enable --now upload.service


            And it will perform the upload when stopped, which you can test with:



            $ sudo systemctl stop upload.service


            This was recently discussed in the systemd-devel mailing list.






            share|improve this answer



























              up vote
              0
              down vote













              Execstop and Execstart reversed



              According to this Unix & Linux answer: How to run a script with systemd right before shutdown, your Execstop and Execstart are reversed.



              The lines:



              ExecStop=/bin/true
              ExecStart=/bin/bash /home/upload.sh


              Should read:



              ExecStart=/bin/true
              ExecStop=/bin/bash /home/upload.sh


              The answer goes on to mention to restart services. So after creating the file, make sure to systemctl daemon-reload and systemctl enable yourservice --now




              Alternate method not working



              The before=, requires=, etc. can be confusing at times. You can put your script in /usr/lib/systemd/system-shutdown/ directory instead. It will be run immediately upon shutdown. See: https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd



              The /usr prefix may be different on some systems, ie just start directory name with /lib.






              share|improve this answer






















              • Please try in your pc,the same error as before.
                – it_is_a_literature
                Apr 17 at 0:36










              • @it_is_a_literature I revised the answer and added systemd method based on your question. The old answer was deprecated to "Alternate" section. Note the answer you accepted appears to have start and stop reversed??
                – WinEunuuchs2Unix
                Apr 22 at 18:34











              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',
              convertImagesToLinks: true,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              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%2f1024886%2fcould-not-resolve-host-content-dropboxapi-com-when-to-upload-file-at-shutdown%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted
              +150










              method 1: /etc/systemd/system/upload.service

              1.1 /etc/systemd/system/upload.service



              sudo vim /etc/systemd/system/upload.service
              [Unit]
              Description=upload files into dropbox
              Before=shutdown.target reboot.target
              Requires=network-online.target
              After=network.target

              [Service]
              ExecStop=/bin/true
              ExecStart=/bin/bash /home/upload.sh
              StandardOutput=journal
              StandardError=journal

              [Install]
              WantedBy=multi-user.target


              1.2 vim /home/upload.sh



              cd /home
              curl -X POST https://content.dropboxapi.com/2/files/upload
              --header "Authorization: Bearer xxxx"
              --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
              --header "Content-Type: application/octet-stream"
              --data-binary @"test.txt"


              1.3 sudo systemctl enable upload service



              method 2: /lib/systemd/system-shutdown/upload.service

              2.1 /lib/systemd/system-shutdown/upload.service



              sudo vim /lib/systemd/system-shutdown/upload.service
              [Unit]
              Description=upload files into dropbox
              Before=shutdown.target reboot.target
              Requires=network-online.target
              After=network.target

              [Service]
              ExecStop=/bin/true
              ExecStart=/bin/bash /home/upload.sh
              StandardOutput=journal
              StandardError=journal

              [Install]
              WantedBy=multi-user.target


              2.2 vim /home/upload.sh



              cd /home
              curl -X POST https://content.dropboxapi.com/2/files/upload
              --header "Authorization: Bearer xxxx"
              --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
              --header "Content-Type: application/octet-stream"
              --data-binary @"test.txt"


              2.3 sudo systemctl enable /lib/systemd/system-shutdown/upload.service






              share|improve this answer


























                up vote
                1
                down vote



                accepted
                +150










                method 1: /etc/systemd/system/upload.service

                1.1 /etc/systemd/system/upload.service



                sudo vim /etc/systemd/system/upload.service
                [Unit]
                Description=upload files into dropbox
                Before=shutdown.target reboot.target
                Requires=network-online.target
                After=network.target

                [Service]
                ExecStop=/bin/true
                ExecStart=/bin/bash /home/upload.sh
                StandardOutput=journal
                StandardError=journal

                [Install]
                WantedBy=multi-user.target


                1.2 vim /home/upload.sh



                cd /home
                curl -X POST https://content.dropboxapi.com/2/files/upload
                --header "Authorization: Bearer xxxx"
                --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
                --header "Content-Type: application/octet-stream"
                --data-binary @"test.txt"


                1.3 sudo systemctl enable upload service



                method 2: /lib/systemd/system-shutdown/upload.service

                2.1 /lib/systemd/system-shutdown/upload.service



                sudo vim /lib/systemd/system-shutdown/upload.service
                [Unit]
                Description=upload files into dropbox
                Before=shutdown.target reboot.target
                Requires=network-online.target
                After=network.target

                [Service]
                ExecStop=/bin/true
                ExecStart=/bin/bash /home/upload.sh
                StandardOutput=journal
                StandardError=journal

                [Install]
                WantedBy=multi-user.target


                2.2 vim /home/upload.sh



                cd /home
                curl -X POST https://content.dropboxapi.com/2/files/upload
                --header "Authorization: Bearer xxxx"
                --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
                --header "Content-Type: application/octet-stream"
                --data-binary @"test.txt"


                2.3 sudo systemctl enable /lib/systemd/system-shutdown/upload.service






                share|improve this answer
























                  up vote
                  1
                  down vote



                  accepted
                  +150







                  up vote
                  1
                  down vote



                  accepted
                  +150




                  +150




                  method 1: /etc/systemd/system/upload.service

                  1.1 /etc/systemd/system/upload.service



                  sudo vim /etc/systemd/system/upload.service
                  [Unit]
                  Description=upload files into dropbox
                  Before=shutdown.target reboot.target
                  Requires=network-online.target
                  After=network.target

                  [Service]
                  ExecStop=/bin/true
                  ExecStart=/bin/bash /home/upload.sh
                  StandardOutput=journal
                  StandardError=journal

                  [Install]
                  WantedBy=multi-user.target


                  1.2 vim /home/upload.sh



                  cd /home
                  curl -X POST https://content.dropboxapi.com/2/files/upload
                  --header "Authorization: Bearer xxxx"
                  --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
                  --header "Content-Type: application/octet-stream"
                  --data-binary @"test.txt"


                  1.3 sudo systemctl enable upload service



                  method 2: /lib/systemd/system-shutdown/upload.service

                  2.1 /lib/systemd/system-shutdown/upload.service



                  sudo vim /lib/systemd/system-shutdown/upload.service
                  [Unit]
                  Description=upload files into dropbox
                  Before=shutdown.target reboot.target
                  Requires=network-online.target
                  After=network.target

                  [Service]
                  ExecStop=/bin/true
                  ExecStart=/bin/bash /home/upload.sh
                  StandardOutput=journal
                  StandardError=journal

                  [Install]
                  WantedBy=multi-user.target


                  2.2 vim /home/upload.sh



                  cd /home
                  curl -X POST https://content.dropboxapi.com/2/files/upload
                  --header "Authorization: Bearer xxxx"
                  --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
                  --header "Content-Type: application/octet-stream"
                  --data-binary @"test.txt"


                  2.3 sudo systemctl enable /lib/systemd/system-shutdown/upload.service






                  share|improve this answer














                  method 1: /etc/systemd/system/upload.service

                  1.1 /etc/systemd/system/upload.service



                  sudo vim /etc/systemd/system/upload.service
                  [Unit]
                  Description=upload files into dropbox
                  Before=shutdown.target reboot.target
                  Requires=network-online.target
                  After=network.target

                  [Service]
                  ExecStop=/bin/true
                  ExecStart=/bin/bash /home/upload.sh
                  StandardOutput=journal
                  StandardError=journal

                  [Install]
                  WantedBy=multi-user.target


                  1.2 vim /home/upload.sh



                  cd /home
                  curl -X POST https://content.dropboxapi.com/2/files/upload
                  --header "Authorization: Bearer xxxx"
                  --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
                  --header "Content-Type: application/octet-stream"
                  --data-binary @"test.txt"


                  1.3 sudo systemctl enable upload service



                  method 2: /lib/systemd/system-shutdown/upload.service

                  2.1 /lib/systemd/system-shutdown/upload.service



                  sudo vim /lib/systemd/system-shutdown/upload.service
                  [Unit]
                  Description=upload files into dropbox
                  Before=shutdown.target reboot.target
                  Requires=network-online.target
                  After=network.target

                  [Service]
                  ExecStop=/bin/true
                  ExecStart=/bin/bash /home/upload.sh
                  StandardOutput=journal
                  StandardError=journal

                  [Install]
                  WantedBy=multi-user.target


                  2.2 vim /home/upload.sh



                  cd /home
                  curl -X POST https://content.dropboxapi.com/2/files/upload
                  --header "Authorization: Bearer xxxx"
                  --header "Dropbox-API-Arg: "path":"/test.txt","mode":".tag":"overwrite""
                  --header "Content-Type: application/octet-stream"
                  --data-binary @"test.txt"


                  2.3 sudo systemctl enable /lib/systemd/system-shutdown/upload.service







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 20 at 10:01

























                  answered Apr 17 at 0:59









                  scrapy

                  2835




                  2835






















                      up vote
                      0
                      down vote













                      The best way to run a program or script while the system is going down is by using the ExecStop= of the unit to run such program. The ExecStart= can either be a dummy script (such as /bin/true) or, in recent versions of systemd, it can even be omitted.



                      Regarding dependencies, you want to place your unit after the services you need, such as network-online.target and perhaps systemd-resolved.service if you're using it for DNS resolution. The reason for that is that systems are stopped at shutdown time in the reverse order in which they are started. So by ordering your service after the dependencies, it means your ExecStop= script will be run before the dependencies are stopped.



                      Something like this should work:



                      [Unit]
                      Description=upload files into dropbox
                      After=network.target systemd-resolved.service
                      Requires=network-online.target

                      [Service]
                      Type=oneshot
                      RemainAfterExit=yes
                      ExecStart=/bin/true
                      ExecStop=/bin/bash /home/upload.sh
                      StandardOutput=journal
                      StandardError=journal

                      [Install]
                      WantedBy=multi-user.target


                      Enable the unit as usual (the --now argument also starts it):



                      $ sudo systemctl enable --now upload.service


                      And it will perform the upload when stopped, which you can test with:



                      $ sudo systemctl stop upload.service


                      This was recently discussed in the systemd-devel mailing list.






                      share|improve this answer
























                        up vote
                        0
                        down vote













                        The best way to run a program or script while the system is going down is by using the ExecStop= of the unit to run such program. The ExecStart= can either be a dummy script (such as /bin/true) or, in recent versions of systemd, it can even be omitted.



                        Regarding dependencies, you want to place your unit after the services you need, such as network-online.target and perhaps systemd-resolved.service if you're using it for DNS resolution. The reason for that is that systems are stopped at shutdown time in the reverse order in which they are started. So by ordering your service after the dependencies, it means your ExecStop= script will be run before the dependencies are stopped.



                        Something like this should work:



                        [Unit]
                        Description=upload files into dropbox
                        After=network.target systemd-resolved.service
                        Requires=network-online.target

                        [Service]
                        Type=oneshot
                        RemainAfterExit=yes
                        ExecStart=/bin/true
                        ExecStop=/bin/bash /home/upload.sh
                        StandardOutput=journal
                        StandardError=journal

                        [Install]
                        WantedBy=multi-user.target


                        Enable the unit as usual (the --now argument also starts it):



                        $ sudo systemctl enable --now upload.service


                        And it will perform the upload when stopped, which you can test with:



                        $ sudo systemctl stop upload.service


                        This was recently discussed in the systemd-devel mailing list.






                        share|improve this answer






















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          The best way to run a program or script while the system is going down is by using the ExecStop= of the unit to run such program. The ExecStart= can either be a dummy script (such as /bin/true) or, in recent versions of systemd, it can even be omitted.



                          Regarding dependencies, you want to place your unit after the services you need, such as network-online.target and perhaps systemd-resolved.service if you're using it for DNS resolution. The reason for that is that systems are stopped at shutdown time in the reverse order in which they are started. So by ordering your service after the dependencies, it means your ExecStop= script will be run before the dependencies are stopped.



                          Something like this should work:



                          [Unit]
                          Description=upload files into dropbox
                          After=network.target systemd-resolved.service
                          Requires=network-online.target

                          [Service]
                          Type=oneshot
                          RemainAfterExit=yes
                          ExecStart=/bin/true
                          ExecStop=/bin/bash /home/upload.sh
                          StandardOutput=journal
                          StandardError=journal

                          [Install]
                          WantedBy=multi-user.target


                          Enable the unit as usual (the --now argument also starts it):



                          $ sudo systemctl enable --now upload.service


                          And it will perform the upload when stopped, which you can test with:



                          $ sudo systemctl stop upload.service


                          This was recently discussed in the systemd-devel mailing list.






                          share|improve this answer












                          The best way to run a program or script while the system is going down is by using the ExecStop= of the unit to run such program. The ExecStart= can either be a dummy script (such as /bin/true) or, in recent versions of systemd, it can even be omitted.



                          Regarding dependencies, you want to place your unit after the services you need, such as network-online.target and perhaps systemd-resolved.service if you're using it for DNS resolution. The reason for that is that systems are stopped at shutdown time in the reverse order in which they are started. So by ordering your service after the dependencies, it means your ExecStop= script will be run before the dependencies are stopped.



                          Something like this should work:



                          [Unit]
                          Description=upload files into dropbox
                          After=network.target systemd-resolved.service
                          Requires=network-online.target

                          [Service]
                          Type=oneshot
                          RemainAfterExit=yes
                          ExecStart=/bin/true
                          ExecStop=/bin/bash /home/upload.sh
                          StandardOutput=journal
                          StandardError=journal

                          [Install]
                          WantedBy=multi-user.target


                          Enable the unit as usual (the --now argument also starts it):



                          $ sudo systemctl enable --now upload.service


                          And it will perform the upload when stopped, which you can test with:



                          $ sudo systemctl stop upload.service


                          This was recently discussed in the systemd-devel mailing list.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Apr 17 at 5:57









                          Filipe Brandenburger

                          5867




                          5867




















                              up vote
                              0
                              down vote













                              Execstop and Execstart reversed



                              According to this Unix & Linux answer: How to run a script with systemd right before shutdown, your Execstop and Execstart are reversed.



                              The lines:



                              ExecStop=/bin/true
                              ExecStart=/bin/bash /home/upload.sh


                              Should read:



                              ExecStart=/bin/true
                              ExecStop=/bin/bash /home/upload.sh


                              The answer goes on to mention to restart services. So after creating the file, make sure to systemctl daemon-reload and systemctl enable yourservice --now




                              Alternate method not working



                              The before=, requires=, etc. can be confusing at times. You can put your script in /usr/lib/systemd/system-shutdown/ directory instead. It will be run immediately upon shutdown. See: https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd



                              The /usr prefix may be different on some systems, ie just start directory name with /lib.






                              share|improve this answer






















                              • Please try in your pc,the same error as before.
                                – it_is_a_literature
                                Apr 17 at 0:36










                              • @it_is_a_literature I revised the answer and added systemd method based on your question. The old answer was deprecated to "Alternate" section. Note the answer you accepted appears to have start and stop reversed??
                                – WinEunuuchs2Unix
                                Apr 22 at 18:34















                              up vote
                              0
                              down vote













                              Execstop and Execstart reversed



                              According to this Unix & Linux answer: How to run a script with systemd right before shutdown, your Execstop and Execstart are reversed.



                              The lines:



                              ExecStop=/bin/true
                              ExecStart=/bin/bash /home/upload.sh


                              Should read:



                              ExecStart=/bin/true
                              ExecStop=/bin/bash /home/upload.sh


                              The answer goes on to mention to restart services. So after creating the file, make sure to systemctl daemon-reload and systemctl enable yourservice --now




                              Alternate method not working



                              The before=, requires=, etc. can be confusing at times. You can put your script in /usr/lib/systemd/system-shutdown/ directory instead. It will be run immediately upon shutdown. See: https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd



                              The /usr prefix may be different on some systems, ie just start directory name with /lib.






                              share|improve this answer






















                              • Please try in your pc,the same error as before.
                                – it_is_a_literature
                                Apr 17 at 0:36










                              • @it_is_a_literature I revised the answer and added systemd method based on your question. The old answer was deprecated to "Alternate" section. Note the answer you accepted appears to have start and stop reversed??
                                – WinEunuuchs2Unix
                                Apr 22 at 18:34













                              up vote
                              0
                              down vote










                              up vote
                              0
                              down vote









                              Execstop and Execstart reversed



                              According to this Unix & Linux answer: How to run a script with systemd right before shutdown, your Execstop and Execstart are reversed.



                              The lines:



                              ExecStop=/bin/true
                              ExecStart=/bin/bash /home/upload.sh


                              Should read:



                              ExecStart=/bin/true
                              ExecStop=/bin/bash /home/upload.sh


                              The answer goes on to mention to restart services. So after creating the file, make sure to systemctl daemon-reload and systemctl enable yourservice --now




                              Alternate method not working



                              The before=, requires=, etc. can be confusing at times. You can put your script in /usr/lib/systemd/system-shutdown/ directory instead. It will be run immediately upon shutdown. See: https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd



                              The /usr prefix may be different on some systems, ie just start directory name with /lib.






                              share|improve this answer














                              Execstop and Execstart reversed



                              According to this Unix & Linux answer: How to run a script with systemd right before shutdown, your Execstop and Execstart are reversed.



                              The lines:



                              ExecStop=/bin/true
                              ExecStart=/bin/bash /home/upload.sh


                              Should read:



                              ExecStart=/bin/true
                              ExecStop=/bin/bash /home/upload.sh


                              The answer goes on to mention to restart services. So after creating the file, make sure to systemctl daemon-reload and systemctl enable yourservice --now




                              Alternate method not working



                              The before=, requires=, etc. can be confusing at times. You can put your script in /usr/lib/systemd/system-shutdown/ directory instead. It will be run immediately upon shutdown. See: https://superuser.com/questions/1016827/how-do-i-run-a-script-before-everything-else-on-shutdown-with-systemd



                              The /usr prefix may be different on some systems, ie just start directory name with /lib.







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Apr 17 at 10:23

























                              answered Apr 16 at 11:38









                              WinEunuuchs2Unix

                              35.8k759133




                              35.8k759133











                              • Please try in your pc,the same error as before.
                                – it_is_a_literature
                                Apr 17 at 0:36










                              • @it_is_a_literature I revised the answer and added systemd method based on your question. The old answer was deprecated to "Alternate" section. Note the answer you accepted appears to have start and stop reversed??
                                – WinEunuuchs2Unix
                                Apr 22 at 18:34

















                              • Please try in your pc,the same error as before.
                                – it_is_a_literature
                                Apr 17 at 0:36










                              • @it_is_a_literature I revised the answer and added systemd method based on your question. The old answer was deprecated to "Alternate" section. Note the answer you accepted appears to have start and stop reversed??
                                – WinEunuuchs2Unix
                                Apr 22 at 18:34
















                              Please try in your pc,the same error as before.
                              – it_is_a_literature
                              Apr 17 at 0:36




                              Please try in your pc,the same error as before.
                              – it_is_a_literature
                              Apr 17 at 0:36












                              @it_is_a_literature I revised the answer and added systemd method based on your question. The old answer was deprecated to "Alternate" section. Note the answer you accepted appears to have start and stop reversed??
                              – WinEunuuchs2Unix
                              Apr 22 at 18:34





                              @it_is_a_literature I revised the answer and added systemd method based on your question. The old answer was deprecated to "Alternate" section. Note the answer you accepted appears to have start and stop reversed??
                              – WinEunuuchs2Unix
                              Apr 22 at 18:34


















                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1024886%2fcould-not-resolve-host-content-dropboxapi-com-when-to-upload-file-at-shutdown%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              Trouble downloading packages list due to a “Hash sum mismatch” error

                              How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

                              Which professions warranted travel in Medieval times?