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)

  • Swap \; for -- {} + at the end. I think it should drastically improve execution time. – mikeserv Feb 8 '15 at 2:35
  • @mikeserv, isn't -delete even better than exec rm {} \;? – Sparhawk Feb 8 '15 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. – mikeserv Feb 8 '15 at 2:37
  • @mikeserv Good point. Portability is something I always forget. – Sparhawk Feb 8 '15 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. – Chris Feb 8 '15 at 2:43
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

  • -delete works good: exec: 3 - 5 minutes delete: 2 seconds – Chris Feb 8 '15 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.