By a show of hands, how many of you like to reboot your servers? I don't see any hands.
Long uptimes are impressive, aren't they? It makes you one of the cool kids to brag about your 853-day uptime on a production system. What's not so cool is that your users like to use /tmp as their personal dumping ground without regard for the overall system's health, or your uptime bragging rights. And rebooting doesn't get rid of user files, only system ones—even that relief is temporary until services restart and users open applications.
Note: An exception to not deleting user temp files after a reboot is to enable
tmp.mount, but that's a topic for a different article. Plus, there are system housekeeping scripts in place for RHEL 7 and later.
It's impossible to force users to comply with the policy of removing files from the /tmp directory in a timely manner. So, what's a frustrated system administrator to do when you have dozens, hundreds, or even thousands of /tmp directories and users to deal with? The answer is to deploy user file housekeeping scripts.
You can create housekeeping scripts and place them in crontab to periodically remove user files from the /tmp directory. It's an unfortunate but necessary service to provide for your users. Most seasoned system administrators will tell you that you shouldn't remove files from /tmp unless you know that they're not being used, though. That's good advice. Some services write lock files to /tmp, some applications use it, and users use it. So, how do you determine which files your housekeeping script can sweep away without any issue?
[ Free download: Advanced Linux commands cheat sheet. ]
How about filtering files by last accessed time? It's a good choice if you have a time limit on files left in /tmp. For example, if you warn your users that files left in the /tmp directory will be removed if they haven't been accessed in two days, on a rolling basis, they should take notice. Using the last accessed time for user files solves the problem if you also exclude files owned by the root user. For example, use:
find /tmp -type f \( ! -user root \) -atime +2
This script displays all files in the /tmp directory not owned by root that have been accessed more than two days ago. Now to add the command's removal switch:
find /tmp -type f \( ! -user root \) -atime +2 -delete
Copy that text into a file, make it executable, and create a crontab entry that runs this script every eight hours. For example, you might add this to your crontab:
* */8 * * * /opt/scripts/tmp.clean.sh
This script and schedule ensure that your /tmp directory is kept relatively free of garbage. It isn't foolproof, however. If a user decides to dump a huge amount of data into the /tmp directory, this action can cause other problems, such as not being able to log into the system via SSH.
Maintaining the /tmp directory isn't easy. Users love to dump files into /tmp and leave them there indefinitely. Fortunately, for repeat offenders, there's always the possibility of locking their account or sending a strongly worded email about losing access to a system until the matter is cleared through their manager. These tactics usually get the user's attention and further offenses are rare.
Sobre o autor
Ken has used Red Hat Linux since 1996 and has written ebooks, whitepapers, actual books, thousands of exam review questions, and hundreds of articles on open source and other topics. Ken also has 20+ years of experience as an enterprise sysadmin with Unix, Linux, Windows, and Virtualization.
Follow him on Twitter: @kenhess for a continuous feed of Sysadmin topics, film, and random rants.
In the evening after Ken replaces his red hat with his foil hat, he writes and makes films with varying degrees of success and acceptance. He is an award-winning filmmaker who constantly tries to convince everyone of his Renaissance Man status, also with varying degrees of success and acceptance.
Mais como este
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
Navegue por canal
Automação
Últimas novidades em automação de TI para empresas de tecnologia, equipes e ambientes
Inteligência artificial
Descubra as atualizações nas plataformas que proporcionam aos clientes executar suas cargas de trabalho de IA em qualquer ambiente
Nuvem híbrida aberta
Veja como construímos um futuro mais flexível com a nuvem híbrida
Segurança
Veja as últimas novidades sobre como reduzimos riscos em ambientes e tecnologias
Edge computing
Saiba quais são as atualizações nas plataformas que simplificam as operações na borda
Infraestrutura
Saiba o que há de mais recente na plataforma Linux empresarial líder mundial
Aplicações
Conheça nossas soluções desenvolvidas para ajudar você a superar os desafios mais complexos de aplicações
Virtualização
O futuro da virtualização empresarial para suas cargas de trabalho on-premise ou na nuvem