Forum: Ruby on Rails How to clear sessions with cron?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sean S. (Guest)
on 2005-12-17 07:22
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? ;-)

Sean
Brad D. (Guest)
on 2005-12-17 07:48
Sean S. wrote:
> Anyone got a snippet that actually works? ;-)

Here is what I use on my Debian box:

find /tmp/ -name "ruby_sess*" -cmin +600 -exec rm {} \;

Works for me :)
Matthew P. (Guest)
on 2005-12-17 09:02
(Received via mailing list)
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? ;-)

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
Sean S. (Guest)
on 2005-12-17 10:24
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? ;-)
>
> 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
Graham Ashton (Guest)
on 2005-12-17 17:36
(Received via mailing list)
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).
Matthew P. (Guest)
on 2005-12-18 08:54
(Received via mailing list)
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.
<grin>

- Matt
Phil Edelbrock (Guest)
on 2006-01-05 23:32
Brad D. wrote:
> Sean S. wrote:
>> Anyone got a snippet that actually works? ;-)
>
> Here is what I use on my Debian box:
>
> find /tmp/ -name "ruby_sess*" -cmin +600 -exec rm {} \;
>
> Works for me :)


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.
Phil Edelbrock (Guest)
on 2006-01-06 00:40
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
Ram (Guest)
on 2007-01-10 18:33
  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.
Ed Hickey (Guest)
on 2007-01-10 18:38
(Received via mailing list)
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
This topic is locked and can not be replied to.