0

I've created on my fresh server a directory structure like this:

/home/$username/$domain/ (for example: /home/domaincom/domain.com/) - public_html - logs - conf - tmp

Everything works well, but unfortunatelly, apache (or PHP) is not purging tmp directory automaticly. :/

Is there a simple way to purge those directories? For example a solution like logrotate, where I set directory to rotate like this: / home / * / * / logs

Problem appeared because on user temp directory has 1562074 files. :/ I'm not sure who (apache, php, external script) should clean this directory and how often.

Apache or php is not obligated to clean unused temp files (mostly sess_...)?

Thanks for any answers. :)

user160403
  • 5
  • 1
  • 3

3 Answers3

1

Assuming linux, write a script to parse the home directory, for each directory go into the tmp directory and remove everything over a certain date.

An example might be

 #!/bin/bash
 for list '/bin/ls -lRt /home/ | grep tmp/' do
  /usr/bin/find $list -atime -10  -type -f -exec rm -f {} # find+ delete files older than ten days
 done

Obviously, you'll want to have some sanity checks in there, and my syntax may not be correct, and so on. You may want to exclude some directories, pass those to the ls command at the beginning..

Once you're happy the script finds and eliminates what you want, set it up as a cron job to run at night.

NickW
  • 10,263
  • 1
  • 20
  • 27
0

A generic solution is nearly impossible. Why? Every user's temporary folder is per-process value. Furthermore, admin can't rely on $HOME/tmp to be user's temporary folder as user may change it, and fact is, we (users) do that quite often by setting $TMP to something else. On top of all that, every process can have different setting that can't be controlled by the user at all...

Assuming you care only about the users' initial values of the $TMP environment variable you could write a script which would go through the list of users, get their default shell (this is yet another complication), find their default setting for $TMP and finally empty those folders. Second alternative is to write a program that logs-in as specific user, reads users's $TMP, and empties the folder...

However, I presume that all you want is to empty temporary files in those folders some web-application uses to store uploaded files. The easiest thing to do is to maintain your own list of those directories in a simple, plain-text file, and write a small script that will go through that list, and delete files that are older than N days...

I would rather rethink the overall structure of directories, and configure lighttpd, or whatever web-server you use, php, and the rest to store temporary files at KNOWN place so you can do cleanup easily. I would configure all of them to store temporary files in, say, /tmp/<domain>/<service> so for example.com you would have: /tmp/example.com/phpbb, /tmp/example.com/redmine, /tmp/example.com/php-sessions . Of course, you have to be careful to set ownership and permissions well.

DejanLekic
  • 304
  • 3
  • 16
0

You changed the default PHP session save path.

On a Debian system, the PHP directive session.gc_probability is set to 0, preventing old sessions from being cleaned up, and a cron job is set up that clears out the old PHP sessions - but only from the default save path!

So you can fix this in one of two ways:

  1. Change the PHP session save path back to its default.
  2. Set session.gc_probability = 1 in php.ini.
Michael Hampton
  • 244,070
  • 43
  • 506
  • 972