How to clear sessions with cron?


#1

Anyone got a quick code snippet I can add to my crontab? Since Rails
doesn’t do any housekeeping, my /tmp directory just keeps getting fatter
and fatter. I found this useless snippet in the Agile book:

find /tmp/ -name ‘ruby_sess*’ -ctime +12h -delete

Fedora Linux complains about the ‘h’ in 12h, then if you remove the ‘h’
he complains about the ‘-delete’ part too which doesn’t even seem to
exist in the man page for find.

Anyone got a snippet that actually works? :wink:

Sean


#2

Sean S. wrote:

Anyone got a snippet that actually works? :wink:

Here is what I use on my Debian box:

find /tmp/ -name “ruby_sess*” -cmin +600 -exec rm {} ;

Works for me :slight_smile:


#3

On Sat, Dec 17, 2005 at 06:22:52AM +0100, Sean S. wrote:

Anyone got a quick code snippet I can add to my crontab? Since Rails
doesn’t do any housekeeping, my /tmp directory just keeps getting fatter
and fatter. I found this useless snippet in the Agile book:

find /tmp/ -name ‘ruby_sess*’ -ctime +12h -delete

Fedora Linux complains about the ‘h’ in 12h, then if you remove the ‘h’

I get a similar warning with the find in Ubuntu Breezy (4.2.22).
Considering that version was released from the GNU project (according to
it’s timestamp in the FTP directory) in June, I’m curious as to what
version
of find was being used in the Rails book.

he complains about the ‘-delete’ part too which doesn’t even seem to
exist in the man page for find.

This option exists in my manpage, and seems to work fine. I suspect
Fedora’s find may be a little out of date.

Anyone got a snippet that actually works? :wink:

find /tmp/ -name ‘ruby_sess*’ -cmin +$((12*60)) -exec rm -f ‘{}’ ;

should be equivalent and be fairly portable across distros (modulo any
shell
unhappiness with the $((12*60)) construct, which may be a bashism).

  • Matt

#4

On Saturday 17 December, Matthew P. wrote:

Considering that version was released from the GNU project (according to
it’s timestamp in the FTP directory) in June, I’m curious as to what version
of find was being used in the Rails book.

The screenshots in the book give it away; it was most likely a version
of
find for BSD (the screenshots indicate that the authors like Macs).


#5

On Sat, Dec 17, 2005 at 10:33:39AM -0500, Graham Ashton wrote:

On Saturday 17 December, Matthew P. wrote:

Considering that version was released from the GNU project (according to
it’s timestamp in the FTP directory) in June, I’m curious as to what version
of find was being used in the Rails book.

The screenshots in the book give it away; it was most likely a version of
find for BSD (the screenshots indicate that the authors like Macs).

An excellent point, and one that I hadn’t considered. Makes perfect
sense,
too. Time to file a bug report on GNU find to add the multiplier
suffixes.

  • Matt

#6

Matthew P. wrote:

On Sat, Dec 17, 2005 at 06:22:52AM +0100, Sean S. wrote:

Anyone got a quick code snippet I can add to my crontab? Since Rails
doesn’t do any housekeeping, my /tmp directory just keeps getting fatter
and fatter. I found this useless snippet in the Agile book:

find /tmp/ -name ‘ruby_sess*’ -ctime +12h -delete

Fedora Linux complains about the ‘h’ in 12h, then if you remove the ‘h’

I get a similar warning with the find in Ubuntu Breezy (4.2.22).
Considering that version was released from the GNU project (according to
it’s timestamp in the FTP directory) in June, I’m curious as to what
version
of find was being used in the Rails book.

he complains about the ‘-delete’ part too which doesn’t even seem to
exist in the man page for find.

This option exists in my manpage, and seems to work fine. I suspect
Fedora’s find may be a little out of date.

Anyone got a snippet that actually works? :wink:

find /tmp/ -name ‘ruby_sess*’ -cmin +$((12*60)) -exec rm -f ‘{}’ ;

should be equivalent and be fairly portable across distros (modulo any
shell
unhappiness with the $((12*60)) construct, which may be a bashism).

  • Matt

As usual, Rails community to the rescue. Both snippets seem to work
just fine. Thank you!

Sean


#7

Brad D. wrote:

Sean S. wrote:

Anyone got a snippet that actually works? :wink:

Here is what I use on my Debian box:

find /tmp/ -name “ruby_sess*” -cmin +600 -exec rm {} ;

Works for me :slight_smile:

Wouldn’t you want to use -amin instead of -cmin? (access vs. created)

In other words, you probably want to delete stale session files that
haven’t been used in a while rather than just deleting files that were
created a while ago. Otherwise you risk dumping people’s sessions who
frequent the site out from under them while they are in use.


#8

Phil Edelbrock wrote:

Wouldn’t you want to use -amin instead of -cmin? (access vs. created)

BTW- We’ve got this thrown in a script file in /etc/cron.daily/ on our
RedHat Enterprise server:

find /tmp/ -name ‘ruby_sess*’ -atime +4 -exec rm {} ;

It removes session files that haven’t been accessed for over 4 days.

Phil


#9

works for me:

class Cronjobs
def self.clear_sessions
Session.delete_all([“updated_at <= ?”,
Time.now.yesterday.strftime(’%Y-%m-%d’)])

end
end

and then in your crontab:

20 0 * * *
/path/to/rails/app/current/script/runner ‘Cronjobs.clear_sessions;’ -e
production

ed


#10

Hi,

I probably should have made this post a new topic. But
is there a good way to clear sessions in general? Should
one always rely on a cron job?

Thanks,
Ram.