Files
server/opt/psa/bin/apache_cleanup.sh
2026-01-07 20:52:11 +01:00

230 lines
4.9 KiB
Bash
Executable File

#!/bin/bash
### Copyright 1999-2025. WebPros International GmbH. All rights reserved.
PRODUCT_NAME="psa"
PRODUCT_ROOT_D="/opt/psa"
PRODUCT_RC_D="/etc/init.d"
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin; export PATH
LD_LIBRARY_PATH=/usr/local/lib:/opt/psa/lib; export LD_LIBRARY_PATH
LANG=C; export LANG
umask 022
# check for root
case "$EUID" in
"0")
;;
*)
echo " $0: This script must be run as root"
echo " Log in as root then run this script again."
echo
exit 1
;;
esac
# library functions
get_pid()
{
local proc_name proc_basename
proc_name="$1"
proc_basename=`basename "$proc_name"`
PID=`ps axww | awk '($5 ~ "('"$proc_name"'|[[(]'"$proc_basename"'[)\\\\]])$") && ($1 > 0) {print $1}'`
echo $PID
}
get_pid_withowner()
{
local proc_name proc_basename owner
proc_name="$1"
owner="$2"
proc_basename=`basename "$proc_name"`
PID=`ps axuww | awk '{if (($1 ~ "$owner") && ($11 ~ "('"$proc_name"'|[[(]'"$proc_basename"'[)\\\\]])$") && ($1 > 0)) {print $2}}'`
echo $PID
}
status()
{
local proc_name="$1"
local pid
pid=`get_pid ${proc_name}`
if [ "X$pid" != "X" ] ; then
echo "${proc_name} (pid $pid) is running..."
return 0
fi
echo "${proc_name} is stopped"
return 1
}
wait_after_stop()
{
PIDS=`echo "$1" | tr ' ' ,`
count=${2:-50}
while [ 0$count -gt 0 ]
do
ps -p $PIDS > /dev/null 2>&1 || break
sleep 1
count=`expr $count - 1`
done
if [ 0$count -eq 0 ]; then
echo "kill remaining processes:"
ps w -p $PIDS 2>/dev/null
kill -9 $1 > /dev/null 2>&1
cleanup_shared_memory
fi
return 0
}
# Remove all semaphores owned by users listed in parameters
cleanup_semaphores()
{
local i n id_n owner_n cmd
# Parse the header and find out on which positions do the owner
# and id appear in ipcs output
n=1
for i in `ipcs -s | awk '$0 !~ /^----/ {s=tolower($0); if (index(s, "id")>0) { print s; exit 0; }}'`; do
if [ "$i" = "id" -o "$i" = "semid" ]; then
id_n='$'$n
fi
if [ "$i" = "owner" ]; then
owner_n='$'$n
fi
n=$(($n+1))
done
if [ "X$id_n" = "X" -o "X$owner_n" = "X" ]; then
echo "WARNING:"
echo "cannot parse ipcs output"
return 1
fi
# Use the parsing results to extract the information with awk and
# remove the selected semaphores
for i in "$@"; do
cmd="`ipcs -s | awk "$owner_n==\\\"$i\\\" { print \\\"-s \\\"$id_n }"`"
if [ -n "$cmd" ]; then
ipcrm $cmd > /dev/null
fi
done
}
# Remove all unused shared memory (whose CPID and LPID are orphaned)
# projects to be excluded are passed as parameters
cleanup_shared_memory()
{
local i n id_n cpid_n lpid_n id cpid lpid exclude_projects exclude_id filter
# Parse the header and find out on which positions do the owner
# and id appear in ipcs output
n=1
for i in `ipcs -m -p | awk '$0 !~ /^----/ {s=tolower($0); if (index(s, "id")>0) { print s; exit 0; } }'`; do
if [ "$i" = "id" -o "$i" = "shmid" ]; then
id_n='$'$n
fi
if [ "$i" = "cpid" ]; then
cpid_n='$'$n
fi
if [ "$i" = "lpid" ]; then
lpid_n='$'$n
fi
n=$(($n+1))
done
if [ "X$id_n" = "X" -o "X$cpid_n" = "X" -o "X$lpid_n" = "X" ]; then
echo "WARNING:"
echo "cannot parse ipcs output"
return 1
fi
# Use the parsing results to extract the information with awk and
# drop the orphaned shared memory resources
if [ $# -ne 0 ]; then
exclude_projects=`echo $* | sed -e 's/ /|^/'`
exclude_id=`ipcs -m | awk 'BEGIN{s=""}END{if(s!="")print s}/^'"$exclude_projects"'/{if(s!="")s=s"|"$2;else s=$2}'`
[ -n "$exclude_id" ] && filter="\$1 !~ /${exclude_id}/ && "
fi
ipcs -m -p | awk "$filter \$3 ~ /[0-9]+/ {print \$1, \$3, \$4}" | while read id cpid lpid; do
if [ ! -d /proc/"$cpid" -a ! -d /proc/"$lpid" ]; then
ipcrm -m "$id" > /dev/null
fi
done
}
cleanup_orphaned_php_fcgi()
{
ps -eo ppid,pid,command | grep -P '^( )+1( )+\d+ .*php.ini$' | awk '{s="kill -9 " $2; system(s);}'
}
warning()
{
file="$1"
service="$2"
echo "WARNING:"
echo "something wrong with file $file"
echo "can't handle $service"
}
stp()
{
service="$1"
echo "$PRODUCT_NAME: $service has been stopped"
}
f_stp()
{
service="$1"
echo "$PRODUCT_NAME: failed to stop $service -- may be it's not running?"
}
rst()
{
service="$1"
echo "$PRODUCT_NAME: $service has been restarted"
}
f_rst()
{
service="$1"
echo "$PRODUCT_NAME: failed to restart $service"
}
strt()
{
service="$1"
echo "$PRODUCT_NAME: $service has been started"
}
f_strt()
{
service="$1"
echo "$PRODUCT_NAME: failed to start $service"
}
grcf()
{
service="$1"
echo "$PRODUCT_NAME: $service has been graceful restarted"
}
f_grcf()
{
service="$1"
echo "$PRODUCT_NAME: failed to graceful restart $service -- may be it's not running?"
}
### Copyright 1999-2025. WebPros International GmbH. All rights reserved.
cleanup_semaphores www-data
cleanup_shared_memory 0x62
cleanup_orphaned_php_fcgi