Friday, January 23, 2015

How to clean unused semaphore

Issue/Symptom  : Sometime you'll see semaphore usage is full.
OS Environment : Linux or RHEL
Software/Application : HP ovo sends semaphore usage alerts
Investigation : Unsed semaphores are not cleared by kernel
Workaround Solution : Use below scripts to clean unused semaphore.


cat clean-unused-semaprhore.sh

#Developed By Kamal maiti, 
#check if root can run it.
if [[ $EUID -ne 0 ]]; then
   exit 1
 else

#collect all semaphore ID
  for SEMID in `ipcs -s|egrep -v -e "Semaphore|key"|sed '/^$/d'|awk '{print $2}'|sort -u`
     do
     #GETPID of semaphore
   PID=`ipcs -s -i $SEMID|tail -2|head -1|awk '{print $NF}'`
     #GET PROCESS ID
    #Ignore process ID 0 which is main process & test PID greater than 0
if [ $PID -gt 0 ]; then

#Test of PID exists in process list, if exits then don't do anything.

  if ps -p $PID > /dev/null
    then
  #running process are
     echo "$SEMID   $PID" &>/dev/null      else
# dead process are, kill corresponding semaphore of related PID is not exisitng.

    echo "$SEMID   $PID" &>/dev/null 

  #cleaning semaphore of dead process :
  ipcrm -s $SEMID
 fi
fi
 done
fi


RUN :chmod +x clean-unused-semaprhore.sh; ./clean-unused-semaprhore.sh

No comments:

Post a Comment