# Deleting expired sessions

**URL:** <https://www.ruby-forum.com/t/deleting-expired-sessions/69899>\
**Category:** Rails\
**Created:** [September 25, 2006, 11:59am UTC](https://www.ruby-forum.com/t/deleting-expired-sessions/69899 "2006-09-25T11:59:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [September 25, 2006, 11:59am UTC](https://www.ruby-forum.com/t/deleting-expired-sessions/69899/1 "2006-09-25T11:59:46Z")

</div>

Hi,

My tmp/sessions directory is getting flooded with files that AFAIK will  
never get removed. I could write a cron job do do this but I was  
wondering if that is really the most elegant and reusable way.

I’d like to create something that could be extracted into a plugin.  
Would it be possible (and wise) to use an after\_filter and backgrounddrb  
to remove expired sessions? Any other suggestions?

Cheers,

Jeroen

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [September 25, 2006, 3:21pm UTC](https://www.ruby-forum.com/t/deleting-expired-sessions/69899/2 "2006-09-25T15:21:03Z")

</div>

Jeroen H. wrote:

> Hi,
> 
> My tmp/sessions directory is getting flooded with files that AFAIK will  
> never get removed. I could write a cron job do do this but I was  
> wondering if that is really the most elegant and reusable way.
> 
> I’d like to create something that could be extracted into a plugin.  
> Would it be possible (and wise) to use an after\_filter and backgrounddrb  
> to remove expired sessions? Any other suggestions?

For what it’s worth we just use a cron job that does find /tmp/sessions  
-name ‘ruby\_sess\*’ -ctime +1 -exec rm -f {}  
Not particularly beautiful but I’m not sure what there is to be gained  
by doing things another way

Fred

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [September 25, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/deleting-expired-sessions/69899/3 "2006-09-25T15:43:31Z")

</div>

Frederick C. wrote:

> Jeroen H. wrote:
> 
> > Hi,
> > 
> > My tmp/sessions directory is getting flooded with files that AFAIK will  
> > never get removed. I could write a cron job do do this but I was  
> > wondering if that is really the most elegant and reusable way.
> > 
> > I’d like to create something that could be extracted into a plugin.  
> > Would it be possible (and wise) to use an after\_filter and backgrounddrb  
> > to remove expired sessions? Any other suggestions?
> 
> For what it’s worth we just use a cron job that does find /tmp/sessions  
> -name ‘ruby\_sess\*’ -ctime +1 -exec rm -f {}  
> Not particularly beautiful but I’m not sure what there is to be gained  
> by doing things another way

Not a great deal, except that you may forget to enable the cron job or  
you may one day decide to change your session name. Simply installing a  
plugin and then being able to forget about it would be nice. I don’t  
really want to use backgroundrb though, after some investigation it  
seems overkill. Think I’ll just go with your solution for now.

Jeroen

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [September 25, 2006, 5:14pm UTC](https://www.ruby-forum.com/t/deleting-expired-sessions/69899/4 "2006-09-25T17:14:53Z")

</div>

“Frederick C.” wrote:

> > to remove expired sessions? Any other suggestions?
> 
> For what it’s worth we just use a cron job that does find /tmp/sessions  
> -name ‘ruby\_sess\*’ -ctime +1 -exec rm -f {}  
> Not particularly beautiful but I’m not sure what there is to be gained  
> by doing things another way

Nice one-liner. However I can’t seem to get it to select files with  
-ctime or -mtime  
less than today (the run day).

find /tmp -name ‘ruby\_sess\*’ -maxdepth 1 -mtime n-whatever

If n-whatever is less than 1 I get the full list of files.  
If n-whatever is \>= +1 I dont get any match.

I only want to remove files that are at least one day old. What am I  
missing?

I also wrote a ruby script recently that does what I want but this is  
one of  
the scripts that is having trouble running under cron (see my post with  
subject  
‘ruby script under cron’).

If anyone is interested in this script I can post it.

Long

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [September 25, 2006, 5:57pm UTC](https://www.ruby-forum.com/t/deleting-expired-sessions/69899/5 "2006-09-25T17:57:03Z")

</div>

Jon G. wrote:

> I also wrote a ruby script recently that does what I want but this is one of  
> the scripts that is having trouble running under cron (see my post with subject  
> ‘ruby script under cron’).
> 
> If anyone is interested in this script I can post it.
> 
> Long

You’re thinking it backwards. -ctime +5 would be files changed 5 days  
ago. ‘man find’ is your friend.

-ctime n  
Fileâ??s status was last changed n\*24 hours ago. See the  
comments  
for -atime to understand how rounding affects the  
interpretation  
of file status change times.

I do that sometimes (thinking backwards :-). I did skim through the man  
pages  
but missed the ‘ago’ part. The n\*24 is crucial here. The files I was  
testing with are  
less than 24 hours AGO, eventhough some were created yesterdays.

It is much clearer now. Thanks Jon.

Long

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [September 25, 2006, 5:28pm UTC](https://www.ruby-forum.com/t/deleting-expired-sessions/69899/6 "2006-09-25T17:28:57Z")

</div>

Long wrote:

> I also wrote a ruby script recently that does what I want but this is one of  
> the scripts that is having trouble running under cron (see my post with subject  
> ‘ruby script under cron’).
> 
> If anyone is interested in this script I can post it.
> 
> Long

You’re thinking it backwards. -ctime +5 would be files changed 5 days  
ago. ‘man find’ is your friend.

-ctime n  
Fileâ??s status was last changed n\*24 hours ago. See the  
comments  
for -atime to understand how rounding affects the  
interpretation  
of file status change times.
