Is it safe to use a bash script and a cronjob to empty
/tmp automatically, or should I do this differently?
/tmp is cleaned on every system reboot, but for systems that cannot be rebooted often, and specially if the only objective is to clean
/tmp a script can be made to clean it.
No process should keep important data there but it is important that you are able to clean
/tmp without disrupting any processes writing data in
I recommend to use
tmpreaper for that.
You can install it with
sudo apt-get install tmpreaper.
From the man page:
tmpreaper recursively searches for and removes files and empty directories which haven't been accessed for a given number of seconds. Normally, it's used to clean up directories which are used for temporary holding space, such as "/tmp". Please read the WARNINGS section of this manual.
Usage normally involves invoking
/tmp with a time spec argument. The time spec argument can be anything from
d for days,
h for hours,
m for minutes, or
s for seconds.
Please use the
--test to dry run and get an output of the results before you actually run the command. That will get you an idea of what will be deleted before you actually commit any changes to the file system.