Run Script once a day with systemd












11














I want to run a backup script 10 minutes after booting up my machine but only once a day. Is it possible to build up such scenarios with systemd?










share|improve this question


















  • 3




    I don't fully understand your requirements, please clarify. I understand that if you reboot your machine serveral times in a day, the script should only run after first reboot, is that right? If your machine runs for more than 1 day long, should the script be run in consecutive days?
    – Krzysztof Adamski
    Sep 14 '12 at 18:29










  • Exactly. I will make a backup every day no matter how often I reboot my PC.
    – Fu86
    Sep 14 '12 at 22:42






  • 5




    If you're not hung up on systemd, this is exactly what anacron is for.
    – Gilles
    Sep 14 '12 at 23:48










  • I may be wrong but there's no easy way to set anacron to run script only 10 minutes after boot is complete.
    – Krzysztof Adamski
    Sep 15 '12 at 9:16










  • That's right. That's why I'm currently using anacron and the good old at, with a daily job like echo "obnam backup" | at "now + 10 minutes"
    – Adrien Clerc
    Sep 14 '13 at 10:33
















11














I want to run a backup script 10 minutes after booting up my machine but only once a day. Is it possible to build up such scenarios with systemd?










share|improve this question


















  • 3




    I don't fully understand your requirements, please clarify. I understand that if you reboot your machine serveral times in a day, the script should only run after first reboot, is that right? If your machine runs for more than 1 day long, should the script be run in consecutive days?
    – Krzysztof Adamski
    Sep 14 '12 at 18:29










  • Exactly. I will make a backup every day no matter how often I reboot my PC.
    – Fu86
    Sep 14 '12 at 22:42






  • 5




    If you're not hung up on systemd, this is exactly what anacron is for.
    – Gilles
    Sep 14 '12 at 23:48










  • I may be wrong but there's no easy way to set anacron to run script only 10 minutes after boot is complete.
    – Krzysztof Adamski
    Sep 15 '12 at 9:16










  • That's right. That's why I'm currently using anacron and the good old at, with a daily job like echo "obnam backup" | at "now + 10 minutes"
    – Adrien Clerc
    Sep 14 '13 at 10:33














11












11








11


2





I want to run a backup script 10 minutes after booting up my machine but only once a day. Is it possible to build up such scenarios with systemd?










share|improve this question













I want to run a backup script 10 minutes after booting up my machine but only once a day. Is it possible to build up such scenarios with systemd?







cron backup systemd






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Sep 14 '12 at 16:43









Fu86

21538




21538








  • 3




    I don't fully understand your requirements, please clarify. I understand that if you reboot your machine serveral times in a day, the script should only run after first reboot, is that right? If your machine runs for more than 1 day long, should the script be run in consecutive days?
    – Krzysztof Adamski
    Sep 14 '12 at 18:29










  • Exactly. I will make a backup every day no matter how often I reboot my PC.
    – Fu86
    Sep 14 '12 at 22:42






  • 5




    If you're not hung up on systemd, this is exactly what anacron is for.
    – Gilles
    Sep 14 '12 at 23:48










  • I may be wrong but there's no easy way to set anacron to run script only 10 minutes after boot is complete.
    – Krzysztof Adamski
    Sep 15 '12 at 9:16










  • That's right. That's why I'm currently using anacron and the good old at, with a daily job like echo "obnam backup" | at "now + 10 minutes"
    – Adrien Clerc
    Sep 14 '13 at 10:33














  • 3




    I don't fully understand your requirements, please clarify. I understand that if you reboot your machine serveral times in a day, the script should only run after first reboot, is that right? If your machine runs for more than 1 day long, should the script be run in consecutive days?
    – Krzysztof Adamski
    Sep 14 '12 at 18:29










  • Exactly. I will make a backup every day no matter how often I reboot my PC.
    – Fu86
    Sep 14 '12 at 22:42






  • 5




    If you're not hung up on systemd, this is exactly what anacron is for.
    – Gilles
    Sep 14 '12 at 23:48










  • I may be wrong but there's no easy way to set anacron to run script only 10 minutes after boot is complete.
    – Krzysztof Adamski
    Sep 15 '12 at 9:16










  • That's right. That's why I'm currently using anacron and the good old at, with a daily job like echo "obnam backup" | at "now + 10 minutes"
    – Adrien Clerc
    Sep 14 '13 at 10:33








3




3




I don't fully understand your requirements, please clarify. I understand that if you reboot your machine serveral times in a day, the script should only run after first reboot, is that right? If your machine runs for more than 1 day long, should the script be run in consecutive days?
– Krzysztof Adamski
Sep 14 '12 at 18:29




I don't fully understand your requirements, please clarify. I understand that if you reboot your machine serveral times in a day, the script should only run after first reboot, is that right? If your machine runs for more than 1 day long, should the script be run in consecutive days?
– Krzysztof Adamski
Sep 14 '12 at 18:29












Exactly. I will make a backup every day no matter how often I reboot my PC.
– Fu86
Sep 14 '12 at 22:42




Exactly. I will make a backup every day no matter how often I reboot my PC.
– Fu86
Sep 14 '12 at 22:42




5




5




If you're not hung up on systemd, this is exactly what anacron is for.
– Gilles
Sep 14 '12 at 23:48




If you're not hung up on systemd, this is exactly what anacron is for.
– Gilles
Sep 14 '12 at 23:48












I may be wrong but there's no easy way to set anacron to run script only 10 minutes after boot is complete.
– Krzysztof Adamski
Sep 15 '12 at 9:16




I may be wrong but there's no easy way to set anacron to run script only 10 minutes after boot is complete.
– Krzysztof Adamski
Sep 15 '12 at 9:16












That's right. That's why I'm currently using anacron and the good old at, with a daily job like echo "obnam backup" | at "now + 10 minutes"
– Adrien Clerc
Sep 14 '13 at 10:33




That's right. That's why I'm currently using anacron and the good old at, with a daily job like echo "obnam backup" | at "now + 10 minutes"
– Adrien Clerc
Sep 14 '13 at 10:33










4 Answers
4






active

oldest

votes


















9














See: /lib/systemd/system/systemd-tmpfiles-clean.timer:



[Timer]
OnBootSec=15min
OnUnitActiveSec=1d


This runs the corresponding .service file 15 minutes after boot and then every day while the system is up. If you reboot multiple times in the same day, you can just have the backup script check the mtime of the archive and skip that run if it's less than a day old.



Also, if your backups are going to be heavy on IO, it might be helpful to read about the IOScheduling* directives in the systemd.exec(5) manpage.






share|improve this answer































    5














    I don't think it's possible to do this entirely in systemd as it does not track any state between reboots. You can easly schedule something to run 10 minutes after boot using systemd.timer and its OnBootSec= or OnStartupSec=.



    The script itself has to check if it was already run today or not. This can be easily implemented even in bash, for example:



    #/bin/sh -x
    FILE=/etc/lastrun
    TODAY=`date +"%Y%m%d"`
    LASTRUN=`cat $FILE`

    [[ -z "$LASTRUN" ]] || [[ "$TODAY" -gt "$LASTRUN" ]] || exit
    echo $TODAY > $FILE
    RUNYOURSCRIPTHERE





    share|improve this answer





























      0














      Apparently this issue will be addressed in future releases. At least according to this email on the systemd-devel mailing list. Until that you have to go the hackish way Krzysztof proposed.






      share|improve this answer





























        0














        If you're not hung up on 10 min after boot, you can:



        [Unit]
        Description=...

        [Timer]
        OnCalendar=daily
        Persistent=true

        [Install]
        WantedBy=timers.target


        OnCalendar=daily makes it run every midnight, Persisten=true on boot if there was at least one missed run.





        share





















          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "106"
          };
          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: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          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%2funix.stackexchange.com%2fquestions%2f48203%2frun-script-once-a-day-with-systemd%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














          See: /lib/systemd/system/systemd-tmpfiles-clean.timer:



          [Timer]
          OnBootSec=15min
          OnUnitActiveSec=1d


          This runs the corresponding .service file 15 minutes after boot and then every day while the system is up. If you reboot multiple times in the same day, you can just have the backup script check the mtime of the archive and skip that run if it's less than a day old.



          Also, if your backups are going to be heavy on IO, it might be helpful to read about the IOScheduling* directives in the systemd.exec(5) manpage.






          share|improve this answer




























            9














            See: /lib/systemd/system/systemd-tmpfiles-clean.timer:



            [Timer]
            OnBootSec=15min
            OnUnitActiveSec=1d


            This runs the corresponding .service file 15 minutes after boot and then every day while the system is up. If you reboot multiple times in the same day, you can just have the backup script check the mtime of the archive and skip that run if it's less than a day old.



            Also, if your backups are going to be heavy on IO, it might be helpful to read about the IOScheduling* directives in the systemd.exec(5) manpage.






            share|improve this answer


























              9












              9








              9






              See: /lib/systemd/system/systemd-tmpfiles-clean.timer:



              [Timer]
              OnBootSec=15min
              OnUnitActiveSec=1d


              This runs the corresponding .service file 15 minutes after boot and then every day while the system is up. If you reboot multiple times in the same day, you can just have the backup script check the mtime of the archive and skip that run if it's less than a day old.



              Also, if your backups are going to be heavy on IO, it might be helpful to read about the IOScheduling* directives in the systemd.exec(5) manpage.






              share|improve this answer














              See: /lib/systemd/system/systemd-tmpfiles-clean.timer:



              [Timer]
              OnBootSec=15min
              OnUnitActiveSec=1d


              This runs the corresponding .service file 15 minutes after boot and then every day while the system is up. If you reboot multiple times in the same day, you can just have the backup script check the mtime of the archive and skip that run if it's less than a day old.



              Also, if your backups are going to be heavy on IO, it might be helpful to read about the IOScheduling* directives in the systemd.exec(5) manpage.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 25 '15 at 22:01









              Evgeny Vereshchagin

              3,24242236




              3,24242236










              answered Feb 8 '13 at 5:36









              Craig

              9112




              9112

























                  5














                  I don't think it's possible to do this entirely in systemd as it does not track any state between reboots. You can easly schedule something to run 10 minutes after boot using systemd.timer and its OnBootSec= or OnStartupSec=.



                  The script itself has to check if it was already run today or not. This can be easily implemented even in bash, for example:



                  #/bin/sh -x
                  FILE=/etc/lastrun
                  TODAY=`date +"%Y%m%d"`
                  LASTRUN=`cat $FILE`

                  [[ -z "$LASTRUN" ]] || [[ "$TODAY" -gt "$LASTRUN" ]] || exit
                  echo $TODAY > $FILE
                  RUNYOURSCRIPTHERE





                  share|improve this answer


























                    5














                    I don't think it's possible to do this entirely in systemd as it does not track any state between reboots. You can easly schedule something to run 10 minutes after boot using systemd.timer and its OnBootSec= or OnStartupSec=.



                    The script itself has to check if it was already run today or not. This can be easily implemented even in bash, for example:



                    #/bin/sh -x
                    FILE=/etc/lastrun
                    TODAY=`date +"%Y%m%d"`
                    LASTRUN=`cat $FILE`

                    [[ -z "$LASTRUN" ]] || [[ "$TODAY" -gt "$LASTRUN" ]] || exit
                    echo $TODAY > $FILE
                    RUNYOURSCRIPTHERE





                    share|improve this answer
























                      5












                      5








                      5






                      I don't think it's possible to do this entirely in systemd as it does not track any state between reboots. You can easly schedule something to run 10 minutes after boot using systemd.timer and its OnBootSec= or OnStartupSec=.



                      The script itself has to check if it was already run today or not. This can be easily implemented even in bash, for example:



                      #/bin/sh -x
                      FILE=/etc/lastrun
                      TODAY=`date +"%Y%m%d"`
                      LASTRUN=`cat $FILE`

                      [[ -z "$LASTRUN" ]] || [[ "$TODAY" -gt "$LASTRUN" ]] || exit
                      echo $TODAY > $FILE
                      RUNYOURSCRIPTHERE





                      share|improve this answer












                      I don't think it's possible to do this entirely in systemd as it does not track any state between reboots. You can easly schedule something to run 10 minutes after boot using systemd.timer and its OnBootSec= or OnStartupSec=.



                      The script itself has to check if it was already run today or not. This can be easily implemented even in bash, for example:



                      #/bin/sh -x
                      FILE=/etc/lastrun
                      TODAY=`date +"%Y%m%d"`
                      LASTRUN=`cat $FILE`

                      [[ -z "$LASTRUN" ]] || [[ "$TODAY" -gt "$LASTRUN" ]] || exit
                      echo $TODAY > $FILE
                      RUNYOURSCRIPTHERE






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Sep 15 '12 at 9:37









                      Krzysztof Adamski

                      3,1681619




                      3,1681619























                          0














                          Apparently this issue will be addressed in future releases. At least according to this email on the systemd-devel mailing list. Until that you have to go the hackish way Krzysztof proposed.






                          share|improve this answer


























                            0














                            Apparently this issue will be addressed in future releases. At least according to this email on the systemd-devel mailing list. Until that you have to go the hackish way Krzysztof proposed.






                            share|improve this answer
























                              0












                              0








                              0






                              Apparently this issue will be addressed in future releases. At least according to this email on the systemd-devel mailing list. Until that you have to go the hackish way Krzysztof proposed.






                              share|improve this answer












                              Apparently this issue will be addressed in future releases. At least according to this email on the systemd-devel mailing list. Until that you have to go the hackish way Krzysztof proposed.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jul 31 '13 at 2:47









                              mmh

                              8915




                              8915























                                  0














                                  If you're not hung up on 10 min after boot, you can:



                                  [Unit]
                                  Description=...

                                  [Timer]
                                  OnCalendar=daily
                                  Persistent=true

                                  [Install]
                                  WantedBy=timers.target


                                  OnCalendar=daily makes it run every midnight, Persisten=true on boot if there was at least one missed run.





                                  share


























                                    0














                                    If you're not hung up on 10 min after boot, you can:



                                    [Unit]
                                    Description=...

                                    [Timer]
                                    OnCalendar=daily
                                    Persistent=true

                                    [Install]
                                    WantedBy=timers.target


                                    OnCalendar=daily makes it run every midnight, Persisten=true on boot if there was at least one missed run.





                                    share
























                                      0












                                      0








                                      0






                                      If you're not hung up on 10 min after boot, you can:



                                      [Unit]
                                      Description=...

                                      [Timer]
                                      OnCalendar=daily
                                      Persistent=true

                                      [Install]
                                      WantedBy=timers.target


                                      OnCalendar=daily makes it run every midnight, Persisten=true on boot if there was at least one missed run.





                                      share












                                      If you're not hung up on 10 min after boot, you can:



                                      [Unit]
                                      Description=...

                                      [Timer]
                                      OnCalendar=daily
                                      Persistent=true

                                      [Install]
                                      WantedBy=timers.target


                                      OnCalendar=daily makes it run every midnight, Persisten=true on boot if there was at least one missed run.






                                      share











                                      share


                                      share










                                      answered 2 mins ago









                                      x-yuri

                                      1,16111642




                                      1,16111642






























                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                          • 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.





                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                          Please pay close attention to the following guidance:


                                          • 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%2funix.stackexchange.com%2fquestions%2f48203%2frun-script-once-a-day-with-systemd%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

                                          Accessing regular linux commands in Huawei's Dopra Linux

                                          Can't connect RFCOMM socket: Host is down

                                          Kernel panic - not syncing: Fatal Exception in Interrupt