P
P
Puma Thailand2012-11-18 16:27:01
linux
Puma Thailand, 2012-11-18 16:27:01

Clearing php sessions in ubuntu and debian

Debian, as I understand it, went its own way and uses a cron script to clean up sessions, which cleans files with old php sessions.
This script looks like this

32093? Ss 0:00 /bin/sh -c [ -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
32098? S 1:54 find /var/lib/php5/ -depth -mindepth 1 -maxdepth 1 -type f -cmin +24! -execdir fuser -s {}; -delete The

big problem is that it heavily loads the screw, I solved it by transferring the directory with sessions to memory via tmpfs

But a second problem appeared, the script does not have time to process the deletion of files and starts again, at times when there are a lot of sessions, several dozen scripts are launched to clean up the sessions and they eat up the entire processor.

Is there any standard way to solve this problem?

version ubuntu 12.04, php standard from the repository.

Answer the question

In order to leave comments, you need to log in

7 answer(s)
M
Melkij, 2012-11-18
@melkij

There are a lot of sessions - how many are they?
Maybe it makes sense to shove them into memcache? Or at least enable directory partitioning (see the description of the setting www.php.net/manual/en/session.configuration.php#ini.session.save-path )?

A
alexeysilver, 2012-11-19
@alexeysilver

We store sessions in redis, I did not find any reason to consider this a bad option. But in your case, I would simply reduce the script startup time, and then the scripts would not intersect.

K
kemko, 2012-11-18
@kemko

The easiest option is to check if there is a file in / tmp before starting the check, if there is, stop, if it is missing, create it, clean up old sessions and then delete it.

V
Vlad Zhivotnev, 2013-11-14
@inkvizitor68sl

Yes, I have. It's called flock.

Use something like this in the cron: 0 0 * * * root flock -n /tmp/somefile.lock "somecsript.sh"

The second flock -n /tmp/somefile.lock "somecsript.sh" will not happen as long as the first instance is running.

In somescript.sh, respectively, shove everything that you are running now (don't forget to chmod +x on the script).

I
impass, 2012-11-18
@impass

If there are too many session files, maybe it's time to somehow optimize your application, and not create a session for each incoming user, regardless of whether it is needed or not?

M
max_rip, 2012-11-19
@max_rip

How many sessions do you have?
If you have several projects on your machine and they are not related to each other, it may make sense to scatter them into different directories, and how often do you run a check?

A
Alexey, 2012-11-20
@alexkbs

It is worth teaching applications not to start sessions when they are not needed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question