Add to Favourites Add to Favourites    Print this Article Print this Article

Automating the removal of apache semaphores with ipcs/ipcrm

If you routinely need to remove apache semaphores with the ipcs/ipcrm tool, and if you cannot sort out why they keep building up, then using a cronjob to automatically clear them out if they've piled up may help.

UPDATE: DirectAdmin 1.53.0+ now has a hook script which can be called, instead of using a cron.  This should provide faster reaction times for a downed service:
https://www.directadmin.com/features.php?id=2101
where you'd use the script below, after confirming the "$service" is "httpd", eg:

if [ "$service" != "httpd" ]; then
       exit 1;
fi

inserted just below the #!/bin.sh line.

Else for the cron method, create the script in:

/etc/cron.hourly/ipcs_check

with the contents:

#!/bin/sh

EMAIL=your@email.com
MAX_SEMAPHORES=15

IPCS=/usr/bin/ipcs
IPCRM=/usr/bin/ipcrm
MAIL=/bin/mail

COUNT=`${IPCS} | grep apache | wc -l`

if [ "$COUNT" -le $MAX_SEMAPHORES ]; then
       #all is well, there are no semaphore build-ups.
       exit 0;
fi

#we have more than MAX_SEMAPHORES, so clear them out and restart Apache.

LIST=/root/sem.txt

${IPCS} | grep apache | awk '{print $2}' > ${LIST}
for i in `cat ${LIST}`; do
{
       ${IPCRM} -s $i;
};
done;

/etc/init.d/httpd restart

TXT="${COUNT} semaphores cleared for apache for `hostname`"
echo "${TXT}" | ${MAIL} -s "${TXT}" ${EMAIL}

exit 1;

and then chmod the script to 755:

chmod 755 /etc/cron.hourly/ipcs_check


Check your /var/log/cron on each hour, to ensure that crond is running it.
If all is well, then there shouldn't be any issues, and the script will exit with a return code of 0.

Was this answer helpful?

Also Read