cronjob for php-session

Discussion in 'Server Operation' started by robertlouwen, Apr 16, 2010.

  1. robertlouwen

    robertlouwen New Member

    Hello all,

    I want session files older than 30 minutes automatic deleted and according to this information found in php.in I have to create cronjob or a script
    Code:
    ; NOTE: If you are using the subdirectory option for storing session files
    ;       (see session.save_path above), then garbage collection does *not*
    ;       happen automatically.  You will need to do your own garbage
    ;       collection through a shell script, cron entry, or some other method.
    ;       For example, the following script would is the equivalent of
    ;       setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
    ;          cd /path/to/sessions; find -cmin +24 | xargs rm
    My path to session is /var/lib/php/session so I tried the following possibilities:
    [root@obelix ~]# crontab -e
    Code:
    */30 * * * * root find -cmin +30 | xargs rm /var/lib/php/session &> /dev/null
    Code:
    */30 * * * * root /var/lib/php/session find -cmin +30 | xargs rm /var/lib/php/session  &> /dev/null
    Code:
    */30 * * * * cd /var/lib/php/session find -cmin +30 | xargs rm &> /dev/null
    None of them work.

    Who knows the right syntax ?
     
  2. falko

    falko Super Moderator Howtoforge Staff

  3. BorderAmigos

    BorderAmigos New Member

    My php set up has this cron job to delete sessions every 30 minutes. In /etc/cron.d/php5 file...

    Code:
    0,30 * * * * root [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -type f -cmin +$(/usr/lib/php5/maxlifetime) -print0 | xargs -n 200 -r -0 rm 
    
    You may want to change php5 to php in your setup.
     
  4. robertlouwen

    robertlouwen New Member

    @ Falko,

    At first I thought "stop this is too difficult for me" my second thought was "a challenge"

    What you say is make a script and make a cronjob for the script.
    From your example I only need
    Code:
    #!/bin/sh
    
    datum=`/bin/date +%Y%m%d-%H`
    
    for file in "$( /usr/bin/find /home/sqlbackup -type f -mtime +2 )"
    do
      /bin/rm -f $file
    done
    
    exit 0
    and adjust this

    Code:
    "$( /usr/bin/find /home/sqlbackup -type f -mtime +2 )"
    do
      /bin/rm -f $file
    to

    Code:
    "$( /usr/bin/find  /var/lib/php/session -type f -cmin +30 )"
    do
      /bin/rm -f $file
    Code:
    chmod 755 /usr/local/sbin/cleanup.sh
    Finaly I create the cronjob
    Code:
    */30 * * * * /usr/local/sbin/cleanup.sh &> /dev/null
    I will give this a try.

    @BorderAmigos,

    I am challenged by Falko's suggestion, if I fail I use your cronjob.
     

Share This Page