Skip to content

Commit

Permalink
Merge pull request #16 from alachaum/improve-php-sessionclean
Browse files Browse the repository at this point in the history
Add more efficient php session cleanup
  • Loading branch information
Arnaud committed Mar 5, 2016
2 parents 3da6703 + 37a2c3a commit cc63ad3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions ansible/roles/apache/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,11 @@
with_items:
- imap

- name: Apache | Session cleaning script /usr/lib/php5/sessionclean
template:
src: php_sessionclean
dest: /usr/lib/php5/sessionclean
owner: root
mode: 0755

- include: ioncube.yml
39 changes: 39 additions & 0 deletions ansible/roles/apache/templates/php_sessionclean
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/sh
# =========================================================================
# This script is more lightweight than the original sessionclean script
# shipped with ubuntu#php5 package which makes use of lsof.
# This Ansible file should eventually removed when the below scripts makes
# it to ubuntu upstream
# See: https://bugs.launchpad.net/ubuntu/+source/php5/+bug/1356113
# =========================================================================

SAPIS="apache2:apache2\napache2filter:apache2\ncgi:php5\nfpm:php5-fpm"

# Iterate through all web SAPIs
(
proc_names=""
echo "$SAPIS" | \
while IFS=: read -r conf_dir proc_name; do
if [ -e /etc/php5/${conf_dir}/php.ini ]; then
# Get all session variables once so we don't need to start PHP to get each config option
session_config=$(php5 -c /etc/php5/${conf_dir}/php.ini -d "error_reporting='~E_ALL'" -r 'foreach(ini_get_all("session") as $k => $v) echo "$k=".$v["local_value"]."\n";')
save_handler=$(echo "$session_config" | sed -ne 's/^session\.save_handler=\(.*\)$/\1/p')
save_path=$(echo "$session_config" | sed -ne 's/^session\.save_path=\(.*\)$/\1/p')
gc_maxlifetime=$(($(echo "$session_config" | sed -ne 's/^session\.gc_maxlifetime=\(.*\)$/\1/p')/60))

if [ "$save_handler" = "files" -a -d "$save_path" ]; then
proc_names="$proc_names $proc_name";
printf "%s:%s\n" "$save_path" "$gc_maxlifetime"
fi
fi
done
# first find all open session files and touch them (hope it's not massive amount of files)
for pid in $(pidof $proc_names); do
find "/proc/$pid/fd" -ignore_readdir_race -lname "$save_path/sess_\*" -exec touch -c {} \;
done
) | sort -rn -t: -k2,2 | sort -u -t: -k 1,1 | while IFS=: read -r save_path gc_maxlifetime; do
# find all files older then maxlifetime and delete them
find -O3 "$save_path" -depth -mindepth 1 -name 'sess_*' -ignore_readdir_race -type f -cmin "+$gc_maxlifetime" -delete
done

exit 0

0 comments on commit cc63ad3

Please sign in to comment.