2

I'm running Magento and it doesn't clean up old session data, so I need to clean it up with a shell script / cronjob:

0,30 * * * * /usr/bin/find /var/www/magento/var/session -name 'sess_*' -type f -mtime +1 -exec rm {} \;

But that script takes usually some minutes to delete the old files. I'm wondering whether I need to search for the files since in this directory are only files that start with "sess_" - but I still need to traverse those files somehow, right?

(around 50.000 files per day are created in this directory)

6
  • Swap \; for -- {} + at the end. I think it should drastically improve execution time. Commented Feb 8, 2015 at 2:35
  • @mikeserv, isn't -delete even better than exec rm {} \;? Commented Feb 8, 2015 at 2:36
  • @Sparhawk - that depends on your find - -exec is portable, -delete is an extension. In my opinion, anything scripted should eschew extensions when practically possible. Commented Feb 8, 2015 at 2:37
  • @mikeserv Good point. Portability is something I always forget. Commented Feb 8, 2015 at 2:38
  • -- {} + at the end didn't really work: Something like: I can only use {} once -delete works good: exec: 3 - 5 minutes delete: 2 seconds And the server load is drastically reduced, which I'm after. Commented Feb 8, 2015 at 2:43

3 Answers 3

1

Don't bother exec'ing rm at all, find can handle it:

0,30 * * * * /usr/bin/find /var/www/magento/var/session -name 'sess_*' -type f -mtime +1 -delete

1
  • -delete works good: exec: 3 - 5 minutes delete: 2 seconds Commented Feb 8, 2015 at 2:47
0

If the only thing in that directory is sess_* files, you can just leave out the -name 'sess_*', and find will traverse through anyway. Test what it would match by removing the -exec part.

$ /usr/bin/find /var/www/magento/var/session -type f -mtime +1

Then for the cron job,

0,30 * * * * /usr/bin/find /var/www/magento/var/session -type f -mtime +1 -exec rm {} \;

Having said that, I'm not sure that this is where the bottleneck is, so perhaps mikeserv's comments would be more helpful.

Also, you should probably use -execdir instead of -exec for security reasons; see man find.

-1

I would use perl, it is faster and more efficient for this than find + rm

0,30 * * * * cd /var/www/magento/var/session && /usr/bin/perl -e 'for(<sess_*>){((stat)[9]<(unlink))}'

Deleting my log directory with over 500,000 logs in it daily clears it out in under ~6 minutes.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.