#!/bin/sh -e SAPIS="/etc/php5/apache2:apache2:php5 /etc/php5/apache2filter:apache2:php5 /etc/php5/cgi:php:php5 /etc/php5/fpm:php5-fpm:php5 /etc/hhvm:hhvm:hhvm\t--php" # Iterate through all web SAPIs ( proc_names="" for sapi in $SAPIS; do conf_dir=$(echo "$sapi" | cut -d: -f1) proc_name=$(echo "$sapi" | cut -d: -f2) php_exec=$(echo "$sapi" | cut -d: -f3) if [ -e ${conf_dir}/php.ini ]; then save_handler=$(${php_exec} -c ${conf_dir}/php.ini -d "error_reporting='~E_ALL'" -r 'echo ini_get("session.save_handler");' | sed -e 's/.*;//') save_path=$(${php_exec} -c ${conf_dir}/php.ini -d "error_reporting='~E_ALL'" -r 'echo ini_get("session.save_path")?: "/tmp";' | sed -e 's/.*;//') gc_maxlifetime=$(${php_exec} -c ${conf_dir}/php.ini -d "error_reporting='~E_ALL'" -r 'echo ini_get("session.gc_maxlifetime")/60;') if [ "$save_handler" = "files" -a -d "$save_path" ]; then proc_names="$proc_names\n$proc_name" echo "$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 $(echo $proc_names | sort -u | xargs pidof); do find "/proc/$pid/fd" -ignore_readdir_race -lname "$save_path/sess_\*" -exec touch -c {} \; done ) | sort -r | 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