How to configure /tmp cleanup?

0216

To configure the cleanup of the /tmp directory in Linux, you can use several methods depending on your distribution and requirements. Here are a few common approaches:

1. Using tmpfs

You can mount /tmp as a tmpfs filesystem, which stores files in memory and automatically clears them on reboot. To do this, add the following line to your /etc/fstab:

tmpfs /tmp tmpfs defaults,noatime,nosuid,size=1G 0 0

This example sets a size limit of 1GB. Adjust the size as needed.

2. Systemd Timer

If your system uses systemd, you can create a timer to clean up /tmp periodically. Create a service file /etc/systemd/system/tmp-cleanup.service:

[Unit]
Description=Cleanup /tmp directory

[Service]
Type=oneshot
ExecStart=/usr/bin/find /tmp -type f -atime +10 -delete

Then create a timer file /etc/systemd/system/tmp-cleanup.timer:

[Unit]
Description=Run tmp-cleanup.service daily

[Timer]
OnCalendar=daily
Persistent=true

[Install]
WantedBy=timers.target

Enable and start the timer:

sudo systemctl enable tmp-cleanup.timer
sudo systemctl start tmp-cleanup.timer

3. Cron Job

You can also set up a cron job to clean up /tmp. Edit the crontab with:

crontab -e

Add a line to run a cleanup command daily:

0 0 * * * /usr/bin/find /tmp -type f -atime +10 -delete

This command deletes files in /tmp that haven't been accessed in the last 10 days.

4. Configuration Files

Some distributions have specific configuration files for managing /tmp cleanup. For example, on Debian-based systems, you can configure /etc/tmpfiles.d/tmp.conf to specify cleanup rules.

Conclusion

Choose the method that best fits your needs and system configuration. Always ensure that important temporary files are not deleted inadvertently.

0 Comments

no data
Be the first to share your comment!