Manage Disk Space with crontab
Learn how to delete unused files in your disk regularly using shell script and crontab.
In my project, users upload a large amount of review and receipt images. They go to files
directory on web servers. It's been more than a year since we launched the service and already received several disk full warnings because of those images.
Writing a Shell Script
To solve this, I wrote a simple file to remove the shell script. There are several ways to achieve this. The straightforward way is
Use the wild card(*) and delete the entire files regardless of what it contains. However, it can cause ‘too many arguments’ error if the amount of files exceeds the possible numbers. To avoid that kind of situation, I used to find -delete command.
exec rm {} \; is also possible instead of using -delete flag.
Check the File Owner
Okay, what next? Shell scripts should be executable. If it’s not, use chown -x command to give a execute permission to user account.
Using crontab to schedule jobs
The last step is the crop job installation. crontab -l command shows the installed cron jobs on the server. Below, I already registered the delete-upload-files script as a cron job, so it’s shown on the list.
0 5 * * *
is called a cron expression. This part is for specifying the schedule when executing the following command.
If you want to edit the cron job list, type crontab -e
to open the vi editor and type whatever the jobs you want or delete the lines which are not needed anymore.
Alternative Way: /etc/cron
There’s another way if you don’t want to specify the execution time using cron expression. Put the shell script at /etc/cron.{hourly|daily|weekly|monthly}
, and there's no need to set the specific time to execute the script. Keep in mind that the script should be executable by the owner who runs the script.
Conclusion
The first script attempt(rm -rf $HOME/files/*
) failed at removing files. I couldn't access the cron logs for diagnosis(my server account didn't have a read privilege somehow), but after googling for a while, I assumed that the reason might be too many files at the file directory. So I tried again with the find
command, and it works well now.
No more disk full warnings! 😁