Clean up massive number of session (sess) files in VestaCP /home/user/tmp/ directory


VestaCP appears to have a problem on Debian/Ubuntu. It doesn’t clean up after itself regarding PHP session files. This results in uncountable files taking up gigabytes of space for no reason. PHP is supposed to clean itself out, but with each user having a different PHP sess file directory, it doesn’t appear to automatically be working.

Nicely enough, PHP comes with an executable script just for this purpose that we can use with cron jobs within the VestaCP.

The easiest way to get those tmp/ directories clean is with the following cron job command:

/usr/bin/nice -n19 /usr/bin/ionice -c2 -n7 /usr/lib/php5/sessionclean /home/USER/tmp/ $(/usr/lib/php5/maxlifetime)

“/usr/bin/nice -n19 /usr/bin/ionice -c2 -n7” sets this job to take a very low priority on CPU and I/O usage, ensuring it doesn’t affect the webhosting availability or speed.

“/usr/lib/php/sessionclean” is the executable, which needs two variables. $1 is the directory to clean, $2 is the number of minutes back to start the removal.

“/home/USER/tmp/” is the directory to clean. Replace USER with the user running the cronjob.

“$(/usr/lib/php5/maxlifetime)” returns 24, unless a different setting is in your php.ini. So any file older than 24 minutes will be purged.

 

You can choose how often to run it, for example: 2 1 * * * shown in the image below means 1:02 in the morning each day.

vestacp-cronjob-sess

Leave a Reply

Your email address will not be published. Required fields are marked *