1

I have folder for temporary folders and I would like it to be emptied automatically at 3am. I know crontab probably could do this but what if my laptop isn't on when it should run?

6
  • Note that deleting a file does not free the space, if it is still opened. Deletion merely unlinks the file from the file system, so that it can not be opened by other commands.
    – ignis
    Oct 14, 2014 at 17:57
  • Interesting question. You can create a daemon process/service such a way the operation is performed when system is turned on.
    – vembutech
    Oct 14, 2014 at 17:59
  • @ignis ?? the rm -r command should delete the files/directory Oct 14, 2014 at 17:59
  • @JacobVlijm Untrue. rm is a wrong name actually, it does unlink. The space occupied by the file is reclaimed only if and when the file is closed by all processes that opened it. This is why you can update the system without rebooting - the daemons refer to the unlinked file until they are restarted and re-open the new file. This also allows an application to safely create a temporary file that will be visible only to it (short of race conditions). That's also why you need to check for unlinked files in lsof when you run out of space. This behavior is notably different from the Windows one.
    – ignis
    Oct 14, 2014 at 18:02
  • @Viped Depending on how you plan to use the system, you may want to mount --bind a subdirectory of /tmp (which is for files and directories that do not have to survive reboot) or mount tmpfs over that directory (con: makes it possible to run out of memory; no pros with respect to keeping it on the filesystem; which is why Debian and ubuntu do not mount tmpfs on /tmp). I'm answering in a comment because your question is quite generic, you have not stated the "real goal" or actual use case and the requirements.
    – ignis
    Oct 14, 2014 at 18:18

1 Answer 1

2

The answer to your problem is Anacron. It comes with Ubuntu by default. Just put your script in one of these folders and make it executable:

/etc/cron.hourly  # run hourly, only available since Ubuntu 14.04
/etc/cron.daily   # run daily
/etc/cron.weekly  # run weekly
/etc/cron.monthly # run monthly

For finetuning check out the anacron manpage. The difference between anacron and crontab is that anacron jobs will run later if they can't run at the specified time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.