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.


3 Answers 3


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.


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

  • does @reboot run the script before shutdown or during the next boot?
    – odorf
    Jun 2, 2017 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, 2017 at 20:16
  • i updated my question. thank you for your answer.
    – odorf
    Jun 2, 2017 at 20:38

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .