Architecture, Devops

How to auto clean Linux files in ubuntu server

I’ve recently developed a function that processes a lot of PDF files. It interacts with the files in AWS S3 bucket. So it needs to download them for processing locally. However, when we do this, it will create a lot of temporary files but I found that the system does not automatically remove them. They consume the memory day by day until I encountered “no disk space” error. This is so bad. The system can not function without memory. I did some research and found “tmpreaper“. So I delete the last 1 day files by this command.

sudo tmpreaper 1d /tmp

It defaults to hours, or you may suffix with s',m’, h', ord’. We also need to point out the directory /tmp. But we can not do manually like this. I’m lazy. So I use the cronjob. In order to use it we need to enable the cron in system.

sudo systemctl enable cron

The structure of crontab is like below.

minute hour day_of_month month day_of_week command_to_run

I want to run this command at 5am so it would be like this.

0 5 * * 1 sudo tmpreaper 1d /tmp

Don’t forget to restart the cron service.

sudo service cron restart

That’s it.

0