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?

Viped
  • 41
  • 3
  • 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 '14 at 17:57
  • Interesting question. You can create a daemon process/service such a way the operation is performed when system is turned on. – BDRSuite Oct 14 '14 at 17:59
  • @ignis ?? the rm -r command should delete the files/directory – Jacob Vlijm Oct 14 '14 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 '14 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 '14 at 18:18
  • @ignis I am used to have some folder where I create files that are needed only temporarily and what directory I can just empty if I need more free space. Reasons are various and but I do lot of programming and every kind of testing. I might build big network with virtualbox and store those virtual hard drives to temp folder etc etc. – Viped Oct 14 '14 at 18:26

1 Answers1

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.

Melebius
  • 11,431
  • 9
  • 52
  • 78
mniess
  • 10,546