Running a script with systemd on shutdown or reboot

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I've made a script which just empties a folder from its contents and then shutdown the pc.



I was wondering if there is a simpler way of doing that automatically when I shutdown or reboot the pc instead of calling the script from the command line. While searching, I found that I probably want a systemd service, but I don't know how to write one and I couldn't find any tutorials on the internet.



After all I just want to run simple single command



rm -rf /my/folder/*


Am I looking the right way, or is there a simpler way to accomplish this?



I want the folder to empty before shutdown and not after the next boot.










share|improve this question






























    1















    I've made a script which just empties a folder from its contents and then shutdown the pc.



    I was wondering if there is a simpler way of doing that automatically when I shutdown or reboot the pc instead of calling the script from the command line. While searching, I found that I probably want a systemd service, but I don't know how to write one and I couldn't find any tutorials on the internet.



    After all I just want to run simple single command



    rm -rf /my/folder/*


    Am I looking the right way, or is there a simpler way to accomplish this?



    I want the folder to empty before shutdown and not after the next boot.










    share|improve this question


























      1












      1








      1








      I've made a script which just empties a folder from its contents and then shutdown the pc.



      I was wondering if there is a simpler way of doing that automatically when I shutdown or reboot the pc instead of calling the script from the command line. While searching, I found that I probably want a systemd service, but I don't know how to write one and I couldn't find any tutorials on the internet.



      After all I just want to run simple single command



      rm -rf /my/folder/*


      Am I looking the right way, or is there a simpler way to accomplish this?



      I want the folder to empty before shutdown and not after the next boot.










      share|improve this question
















      I've made a script which just empties a folder from its contents and then shutdown the pc.



      I was wondering if there is a simpler way of doing that automatically when I shutdown or reboot the pc instead of calling the script from the command line. While searching, I found that I probably want a systemd service, but I don't know how to write one and I couldn't find any tutorials on the internet.



      After all I just want to run simple single command



      rm -rf /my/folder/*


      Am I looking the right way, or is there a simpler way to accomplish this?



      I want the folder to empty before shutdown and not after the next boot.







      systemd shutdown reboot






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 18 at 2:06









      Rui F Ribeiro

      42.1k1484142




      42.1k1484142










      asked Jun 2 '17 at 17:13









      odorfodorf

      112




      112




















          3 Answers
          3






          active

          oldest

          votes


















          2














          This sounds like you are manually creating and managing a temporary directory. If that's the case, check the systemd docs for how it can help with this. See man tmpfiles.d for details.






          share|improve this answer






























            1














            Set up a cron table entry with a schedule of @reboot to remove and/or recreate the directories in question.






            share|improve this answer























            • does @reboot run the script before shutdown or during the next boot?

              – odorf
              Jun 2 '17 at 19:35











            • It runs at system startup, since shutdown kills the cron daemon, it cannot tell the kernel "okey dokey, let me just start one new job.."

              – DopeGhoti
              Jun 2 '17 at 20:16











            • i updated my question. thank you for your answer.

              – odorf
              Jun 2 '17 at 20:38


















            0














            On Centos/RHEL 7 there still is a /etc/rc.d/rc.local which uses systemd. I suspect that the same is true on other Linux distributions. This will run the rc.local scripting at reboot. There is significant commenting and warnings in the file that you should really use the systemd type of run structure.






            share|improve this answer























              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%2f368847%2frunning-a-script-with-systemd-on-shutdown-or-reboot%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              This sounds like you are manually creating and managing a temporary directory. If that's the case, check the systemd docs for how it can help with this. See man tmpfiles.d for details.






              share|improve this answer



























                2














                This sounds like you are manually creating and managing a temporary directory. If that's the case, check the systemd docs for how it can help with this. See man tmpfiles.d for details.






                share|improve this answer

























                  2












                  2








                  2







                  This sounds like you are manually creating and managing a temporary directory. If that's the case, check the systemd docs for how it can help with this. See man tmpfiles.d for details.






                  share|improve this answer













                  This sounds like you are manually creating and managing a temporary directory. If that's the case, check the systemd docs for how it can help with this. See man tmpfiles.d for details.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 2 '17 at 17:26









                  Mark StosbergMark Stosberg

                  4,0681226




                  4,0681226























                      1














                      Set up a cron table entry with a schedule of @reboot to remove and/or recreate the directories in question.






                      share|improve this answer























                      • does @reboot run the script before shutdown or during the next boot?

                        – odorf
                        Jun 2 '17 at 19:35











                      • It runs at system startup, since shutdown kills the cron daemon, it cannot tell the kernel "okey dokey, let me just start one new job.."

                        – DopeGhoti
                        Jun 2 '17 at 20:16











                      • i updated my question. thank you for your answer.

                        – odorf
                        Jun 2 '17 at 20:38















                      1














                      Set up a cron table entry with a schedule of @reboot to remove and/or recreate the directories in question.






                      share|improve this answer























                      • does @reboot run the script before shutdown or during the next boot?

                        – odorf
                        Jun 2 '17 at 19:35











                      • It runs at system startup, since shutdown kills the cron daemon, it cannot tell the kernel "okey dokey, let me just start one new job.."

                        – DopeGhoti
                        Jun 2 '17 at 20:16











                      • i updated my question. thank you for your answer.

                        – odorf
                        Jun 2 '17 at 20:38













                      1












                      1








                      1







                      Set up a cron table entry with a schedule of @reboot to remove and/or recreate the directories in question.






                      share|improve this answer













                      Set up a cron table entry with a schedule of @reboot to remove and/or recreate the directories in question.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 2 '17 at 17:39









                      DopeGhotiDopeGhoti

                      47.1k56191




                      47.1k56191












                      • does @reboot run the script before shutdown or during the next boot?

                        – odorf
                        Jun 2 '17 at 19:35











                      • It runs at system startup, since shutdown kills the cron daemon, it cannot tell the kernel "okey dokey, let me just start one new job.."

                        – DopeGhoti
                        Jun 2 '17 at 20:16











                      • i updated my question. thank you for your answer.

                        – odorf
                        Jun 2 '17 at 20:38

















                      • does @reboot run the script before shutdown or during the next boot?

                        – odorf
                        Jun 2 '17 at 19:35











                      • It runs at system startup, since shutdown kills the cron daemon, it cannot tell the kernel "okey dokey, let me just start one new job.."

                        – DopeGhoti
                        Jun 2 '17 at 20:16











                      • i updated my question. thank you for your answer.

                        – odorf
                        Jun 2 '17 at 20:38
















                      does @reboot run the script before shutdown or during the next boot?

                      – odorf
                      Jun 2 '17 at 19:35





                      does @reboot run the script before shutdown or during the next boot?

                      – odorf
                      Jun 2 '17 at 19:35













                      It runs at system startup, since shutdown kills the cron daemon, it cannot tell the kernel "okey dokey, let me just start one new job.."

                      – DopeGhoti
                      Jun 2 '17 at 20:16





                      It runs at system startup, since shutdown kills the cron daemon, it cannot tell the kernel "okey dokey, let me just start one new job.."

                      – DopeGhoti
                      Jun 2 '17 at 20:16













                      i updated my question. thank you for your answer.

                      – odorf
                      Jun 2 '17 at 20:38





                      i updated my question. thank you for your answer.

                      – odorf
                      Jun 2 '17 at 20:38











                      0














                      On Centos/RHEL 7 there still is a /etc/rc.d/rc.local which uses systemd. I suspect that the same is true on other Linux distributions. This will run the rc.local scripting at reboot. There is significant commenting and warnings in the file that you should really use the systemd type of run structure.






                      share|improve this answer



























                        0














                        On Centos/RHEL 7 there still is a /etc/rc.d/rc.local which uses systemd. I suspect that the same is true on other Linux distributions. This will run the rc.local scripting at reboot. There is significant commenting and warnings in the file that you should really use the systemd type of run structure.






                        share|improve this answer

























                          0












                          0








                          0







                          On Centos/RHEL 7 there still is a /etc/rc.d/rc.local which uses systemd. I suspect that the same is true on other Linux distributions. This will run the rc.local scripting at reboot. There is significant commenting and warnings in the file that you should really use the systemd type of run structure.






                          share|improve this answer













                          On Centos/RHEL 7 there still is a /etc/rc.d/rc.local which uses systemd. I suspect that the same is true on other Linux distributions. This will run the rc.local scripting at reboot. There is significant commenting and warnings in the file that you should really use the systemd type of run structure.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jun 2 '17 at 23:22









                          mdpcmdpc

                          5,11121838




                          5,11121838



























                              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.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f368847%2frunning-a-script-with-systemd-on-shutdown-or-reboot%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

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay