Clearing temporary files using tmpreaper
Is your server getting full?
My server was clogged up and had no space.
At first, I thought there was something wrong with my api code, as I was unable to make requests and kept recieving
500 errors from nginx.
Upon further investagion, my
/tmp file was absolutely full (Bare in mind I am using a $5 Digital ocean droplet). I learnt that linux servers clears the
/tmp folder upon restart, however my api needs to handle large files constantly. And restarting my server contstantly was not an option.
After some research, I found out that there was linux package called
tmpreaper, which helps clear folders with files which are older than a specified time. For example:
tmpreaper 1d /tmp
This will clear all files within the
/tmp folder which is a day old.
tmpreaper adds a daily cron job which will clear all temporary files within the
/tmp folder everyday. For my instance, this was not sufficient, I would need files removed that 3-5 minutes old (I wouldn't need these files after that).
In order to do this, I need to create a bash script:
#! /bin/bash tmpreaper 3m /tmp
Then, after running
crontab -e, I added a job to execute this:
* * * * * bash /path/to/script.sh
Note: It's important to make sure your bash scripts are executable using the following command:
chmod +x /path/to/script.sh
Now, my temporary folder is always cleared ready for new content.
I hope this helps.