Find the answer to your Linux question:
Results 1 to 2 of 2
Hello, I have server on which is Plesk last version, CentOS 6.5 64 bit and PHP 5.3.3 installed. I have big problem with php sessions stored at var/lib/php/session . Every ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2008
    Posts
    15

    Exclamation Problem with folder var/lib/php/session size more than 4Gb


    Hello,
    I have server on which is Plesk last version, CentOS 6.5 64 bit and PHP 5.3.3 installed. I have big problem with php sessions stored at var/lib/php/session . Every created session stay in this folder and don't auto delete after expire.
    Can you help me and tell me how to make some script or code to auto delete expired session files?

    And how that file to add into cron to execute example every day at some time.

    Thank you very much

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    hi,

    i'm not sure how to determine whether your session files have expired - there is hopefully a way to do that w/in the configuration of PHP itself.

    for the sake of the argument, let's say that files that haven't been modified in 24 hours are considered expired. then you could run this command to delete those files:
    Code:
    find /var/lib/php/session/ -name $FILE_NAME_REGEX -type f -mmin +1440 -delete
    Note: don't just run that command. understand that it will delete files that match the FILE_NAME_REGEX you give it.

    so once you have a find/delete command with which you are happy, create a script containing it. for example, su to root and create a file in /root called "del-expired-php-files.sh". in it put:

    Code:
    #!/bin/bash
    # command(s) to delete expired files
    find /var/lib/php/session/ -name $FILE_NAME_REGEX -type f -mmin +1440 -delete
    once you are happy with the script, save it and make it executable (chmod +x del-expired-php-files.sh). you can execute it once, to make sure it works as expected.

    now call that script in a cronjob that will run at an interval of your choosing. to do that, still as root, do:

    Code:
    cd /etc/cron.d/
    in this dir, create a file, call it "clean-expired-php-files", and in it put:

    Code:
    # run command every nite at 10pm to clean up files
    0 22 * * * root /root/del-expired-php-files.sh > /tmp/del-files.log 2>&1
    save that file. now the cron daemon, which comes to life every minute in order to execute cron jobs, will see that cronjob and will run it at the 10pm mark. any script output (errors, etc.) will be in the /tmp/del-files.log that I included.

    note: to trouble-shoot the cron stuff, use this string for the time (instead of 0 22 * * *), to run it every minute:

    Code:
    * * * * * root ...
    Note: if the /etc/cron.d directory does not exist on your system, then just create the cronjob as root using this command;

    Code:
    crontab -e
    and enter the same code in it EXCEPT for the username (root), e.g.:

    Code:
    0 22 * * * /root/del-expired-php-files.sh > /tmp/del-files.log 2>&1
    you leave off the username b/c the username is implied when you are editing (-e) your own crontab (and in this case, you are root).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •