Skip to content

Handling or Removing Magento Session Files Using Cron

  • by

Engine: Magento
Server: Apache on Ubuntu
Command: SSH

I’m actually quite shocked that Magento doesn’t have some more in-depth documentation/solution about the fact that their session files can get out of control pretty damn quickly, but I managed to find a few forums and blogs about this problem, and by adding a few solutions together I have solved this issue.

Background

I was creating a backup archive of all of my website files – a smart thing to do on the regular – and this time I decided to do it in verbose mode (-v prints everything to screen). Well after watching about a kajillion session files float past my terminal window I decided I’d look into this.

It turns out that session files are fairly self explanatory: they are like cookies where the keep certain information about a website visitor’s session (things like items in a shopping cart, locale, etc.). So, if you have 30k unique visitors in a day and even just a handful of them have their own unique session file… well. it gets big.

Theoretically php.ini should do the cleanup for you (see Toby’s Ramblings for a quick php garbage collection theory on Magento session files). But, since Magento has them buried into the farthest reaches of the universe, it’s advisable to tell your server to clean them up.

Without having your cron doing some cleanup of those files, it can actually get very big. Of course, the size does depend on the amount of visitors you have, and for small stores it doesn’t get that out of hand, that quick. But, best practice is what we’re here for.

(side note: each session file is actually quite small and, for example, 7000 session files is nothing for Apache to handle)

What is a cron task?

Cron is a system daemon used to execute desired tasks (in the background) at designated times.

Setting up a System Cron Task to do House Cleaning

Like I said before, it’s silly that Magento doesn’t have cron tasks built in to do this automatically (if you know of a way that there is, please tell me how in the comments below).

So, we’re going to set up a system cron task from the root user to do some house cleaning.

The proper way (perhaps the only way) to make or add cron tasks is to open the crontab. We’re going to have this task run as the root, so use sudo

sudo crontab -e

The format of a cron task is (see Ubuntu cron HowTo for more detailed information)

01 04 1 1 1 /usr/bin/somedirectory/somecommand -options -here

where the numbers represent

minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday)

With this in mind, we want to tell cron to find all session files and remove the ones that are older than maxlifetime.

It is up to you to define how frequently you do this (I chose to do it every evening which is probably a bit overkill), but the command to use is

0 1 * * * /usr/bin/find /home/myuser/public_html/var/session -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 -exec rm {} \; >/dev/null 2>&1

this command says at 0100 hrs every day execute the find command looking only in the folder /home/myuser/public_html/var/session (-mindepth -maxdepth) for a file (-type f) that has been alive and existed for longer than maxlifetime (-cmin) and remove it (-exec rm {}) .

What you need to do now is change

 /home/myuser/public_html/var/session

to

 /root/location/of/magento/var/session

This tells the cron job exactly where the session files are located.

Make a different line in the crontab for each instance of a Magento store running.

(note: crontab files must be finished with a new line, so make sure you add that to the end)

Saving Cron Jobs

When you invoke the crontab using the command given above (sudo crontab -e) it will handle where to save the new cron job you just created (or appended to). It is advised not to change the folder path of where you want to save it because the system will take care of this for you. I have always found with system critical files like this, if I try and get fancy and put the files into nice and tidy folders that I’ve created myself, nothing ever works as it should.

So, upon exixting using WriteOut there will be a note something along the lines of

crontab: installing new crontab

depending on how you save/append on exit.

Hope this helps, and please, if you have anything to add to this fairly interesting (and hot topic) please add in the comments below so we can all learn.

Special shout out to a (or some) developer/tester at Sonassi who has had quite in depth discussions about this in various forums (particularly the megabase forum, which I’d highly recommend reading to fully understand the difference between using a db, or file for the session, since Magento allows both) around the interwebs.

References:

Leave a Reply

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

17 + 19 =