4

Possible Duplicate:
Millions of files in php’s tmp error - how to delete?

Is there any way how to delete folder/files quicker than with command rm -rf?

It seems my disc is filled with bilions of files (sessions of php5) which were not deleted in cron so I need to delete them manually but it takes hours and it is still not helping reducing the amount. Thank you.

My command: rm -rf /var/lib/php5/*

Tried also these commands:

find /var/lib/php5 -name "sess_*" -exec rm {} \;

And

perl -e 'chdir "/var/lib/php5/" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'
Byakugan
  • 141
  • 1
  • 3
  • 12

2 Answers2

3

The fastest way is probably:

cd /var/lib/php5
ls -f | xargs -d "\n" rm

Also:

cd /var/lib/php5
for i in {1..999}
do
   find . -type f | head -1000 | xargs rm
done

And, if you like perl:

perl -e 'chdir "/var/lib/php5" or die; opendir D, "."; while ($n = readdir D) { unlink $n }'
David Schwartz
  • 31,449
  • 2
  • 55
  • 84
  • Already trying these seems quicker but still like 2% in one hour so I will leave it to process through night I think ... – Byakugan Nov 15 '12 at 04:15
2

php5 should come with a default cron job to remove session files.

In Debian/Ubuntu, it is like the following one (direct copy from Ubuntu 12.04 LTS)

/etc/cron.d/php5

# /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/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete

It run every half an hour, and remove expired session base on session.gc_maxlifetime in php.ini.

So you should do following:

  1. Check if you have the above cron job file. Add it if missing.
  2. Check value of session.gc_maxlifetime in /etc/php5/apache2/php.ini

    Default value of session.gc_maxlifetime on Ubuntu is 1440sec = 24min

    session.gc_maxlifetime = 1440
    
  3. If the above 2 looks normal, try run the command line in cron job manually. That will print all error on screen.

  4. Grep for cron error in /var/log/syslog. See if they are php related.

Regarding the billions session files already exist, you have to delete them manually for now.

To put the current situation under control

service apache2 stop
mv /var/lib/php5 /var/lib/php5.delete
mkdir /var/lib/php5
chmod 733 /var/lib/php5
chmod o+t /var/lib/php5
service apache2 start

Then deleted /var/lib/php5.delete. It may takes hours. At the same time, keep an eye on file number in new /var/lib/php5 directory. If it is increasing in an abnormal way, you properly have problem other than removing files.

Run cron job command line manually

Just put the portion after root in command prompt, as follow

[ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) ! -execdir fuser -s {} 2>/dev/null \; -delete
John Siu
  • 3,667
  • 2
  • 17
  • 23
  • "Regarding the billions session files already exist, you just have to delete them manually for now." But how that is my question ... My rm -rf command is running like 45 mins and so far deleted 3% o files in this folder so how to do it that is my original question here ... – Byakugan Nov 15 '12 at 04:06
  • BTW my cronjob is like this `09,39 * * * * root [ -x /usr/lib/php5/maxlifetime ] && [ -d /var/lib/php5 ] && find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +$(/usr/lib/php5/maxlifetime) -delete`and it is not working ... – Byakugan Nov 15 '12 at 04:11
  • It is not working because it is already out of control. The direct expansion is taking too long and cron job seems to have a max time limit of 1hr. I added a work around in my answer. That will also let you know if session files is increasing in an abnormal way. – John Siu Nov 15 '12 at 04:19
  • Thank you trying that ... seems normal this folder now ... Thank you. About the cron ... how can I run it manually in command line the line you posted above? To see any errors ... – Byakugan Nov 15 '12 at 04:24
  • @Byakugan I added at the end. Even it gives no error, you still should keep an eye on files number in that dir, maybe for a few days. Also make sure you did the **chmod o+t** for the new folder. – John Siu Nov 15 '12 at 04:29
  • Now no files are created and sessions (apache php based session and login on page) seems to not working now ... Did I delete anything important in this folder? It is not working even with the chmod option you mentioned ... With chnod 777 it is working now .... 10 files created in 2 mins ... Is that ok? – Byakugan Nov 15 '12 at 04:36
  • @Byakugan Restart apache will fix it. The reason apache fail now, even after chmod, is because it ALREADY open the directory with WRONG permission and staying that way. – John Siu Nov 15 '12 at 04:40
  • With chnod 777 it is working now .... 10 files created in 2 mins ... Is that ok or is that too many? – Byakugan Nov 15 '12 at 04:41
  • @Byakugan 10 files is VERY normal. Also you should judge that base on your site traffic, number of users. You need historical data. for that. – John Siu Nov 15 '12 at 04:42
  • Ok seems ok now thank you for everything ... accepting now! One more thing should I leave chod 777 of this new folder or should I use other less dangerous number? – Byakugan Nov 15 '12 at 04:44
  • @Byakugan Try change it back to 755 and +t, that is the original permission. Then restart apache. It should works. If no, **chown :www-data /var/lib/php5**. – John Siu Nov 15 '12 at 04:48
  • @Byakugan I was wrong, it should be **chmod 733 /var/lib/php5**. I updated my answer. user:group should remain root:root. – John Siu Nov 15 '12 at 04:55