Debian based systems Session killed at 30 minutes in special cron, how to override?

15,673

Solution 1

Edit the file /usr/lib/php5/maxlifetime

The value should be in seconds. This file will actually also check your php.ini so I don't know why it wasn't working for you.

Solution 2

This is a question for serverfault.com.

However, change session.gc_maxlifetime in /etc/php5/apache2/php.ini or - if you don't have an apache2 one - one of the other /etc/php5/*/php.ini files. The script /usr/lib/php5/maxlifetime will then use the maximum for that setting found in any of those files.

Editing maxlifetime won't help or at least only until the php5-common package is updated again.

Solution 3

You can provide your own session path session.save_path OR use a different handler altogether session.save_handler

You'll however need to provide appropriate mechanism for managing unwanted session files.

Found this in my php.ini

; 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

I recently ran into this problem where unwanted session files were accumulating because I was using PHP and mod_fcgid with a custom session.save_path for each virtualhost.

Solution 4

If you came here because your cron is throwing errors every 30 Minutes (at 09 and 39) you may have simmilar errors in your syslog and/or mailbox:

[ -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
PHP Fatal error: Directive 'allow_call_time_pass_reference' is no longer available in PHP in Unknown on line 0

The reason for this maybe that you upgraded your Debian to Wheezy and you have old entrys in your /etc/php5/apache2/php.ini.

I had to comment out the following lines and the errors vanished.

  • allow_call_time_pass_reference
  • register_long_arrays

Im writing this because this is one of the top google results if you search for the error-messages and it may affect many users/admins who maintained thier debian-installations for more than one release.

PS: This helped me very much: http://vernontbludgeon.com/blog/archives/2013/10/debian-php-session-garbage-collection-maxlifetime-fails-when-php.ini-has-obsolete-directives.html

Share:
15,673
Phill Pafford
Author by

Phill Pafford

Love development with PHP/Symfony/PHPStorm, iOS, PostgreSQL, Linux flavor Ubuntu, jQuery/Mobile, Foundation CSS, GitFlow AVH and HTML5 Personal Projects are Crypto Currencies, Home Automation, Mobile development, SMS/MMS and DIY electronics via Make and Hack A Day https://keybase.io/phillpafford https://onename.com/phillpafford #bitcoin: https://www.coinbase.com/phillpafford #DogeCoin: D67fwUKwKQQeL9pdbZmbWcevuAYW8XPqyz

Updated on June 13, 2022

Comments

  • Phill Pafford
    Phill Pafford almost 2 years

    Have been pulling out my hair trying to find out why my sessions are being terminated/killed/destroyed at 30 minutes. Well it looks like Debian based systems have a special cron running that ignores all php.ini and apache configurations and kills any idle session at 30 minutes.

    The cron path: /etc/cron.d/php5

    Inside the cron:

    # /etc/cron.d/php5: crontab fragment for php5
    #  This purges session files older than X, where X is defined in seconds
    #  as the largest value of session.gc_maxlifetime from all your php.ini
    #  files, or 24 minutes if not defined.  See /usr/lib/php5/maxlifetime
    
    # Look for and purge old sessions every 30 minutes
    09,39 *     * * *     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
    

    I'm not bad at configuring and setting up hosts but I'm no sysAdmin. Could someone please help me override/edit/change/reconfigure this so I can set the value longer? I think 3 hours would be nice but I would like to understand the changes so if someone higher up wants to make the session time shorter/longer I con document how to configure the change.

    Thanks to any insight help on this

    EDIT: Adding /usr/lib/php5/maxlifetime code

    #!/bin/sh -e
    
    max=1440
    
    for ini in /etc/php5/*/php.ini; do
            cur=$(sed -n -e 's/^[[:space:]]*session.gc_maxlifetime[[:space:]]*=[[:space:]]*\([0-9]\+\).*$/\1/p' $ini 2>/dev/null || true);
            [ -z "$cur" ] && cur=0
            [ "$cur" -gt "$max" ] && max=$cur
    done
    
    echo $(($max/60))
    
    exit 0
    

    so it looks to be searching all the php.ini files, finds the greatest value, compares it to 1440 (which is 24 minutes).

    Here are the php.ini files

    /etc/php5/apache2/php.ini
    session.gc_maxlifetime = 1440 
    
    /etc/php5/cgi/php.ini
    session.gc_maxlifetime = 1440
    
    /etc/php5/cli/php.ini
    session.gc_maxlifetime = 1440
    

    but why does my script session get killed at 30 minutes and not 24 minutes?

    EDIT #2: CRON running every 30 minutes is why the session looks to be killed at 30 minute intervals. But it could also be 24 to 54 minutes, FYI

    Also looking over the code in: /usr/lib/php5/maxlifetime it's taking the highest value and during my testing I was trying to lower the threshold to speed up the condition.

    Looks like I just need to increase one on the php.ini files to over one hour test test.

  • Phill Pafford
    Phill Pafford over 13 years
    That's what I thought as well but that file searches the php.ini files (all of them) and finds the greatest value in second (which is 1440 or 24 minutes). But my session doesn't timeout till 30 minutes and I can't figure out why
  • al.
    al. over 13 years
    The cron job is only run every 30 minutes, so in fact your session can be valid for 24 to 54 minutes.
  • Phill Pafford
    Phill Pafford over 13 years
    understandable about serverfault.com but I also think this is relevant because I have set the php.ini file as well as trying to set the value within php itself and it still kills the session at 30 minutes, even when everything is configured to 1440 seconds or 24 minutes
  • Phill Pafford
    Phill Pafford over 13 years
    crap, just had a DUH moment. Thanks I think this will help me in setting a longer session.
  • Daniel Soublett
    Daniel Soublett about 8 years
    I'm having this problem too, How can I edit /usr/lib/php5/maxlifetime using putty? I'm really new at it so if you guide me with some commands, would be really helpful.