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

2 comments:

  1. + [[ 514 -ne 514 ]]
    ++ ipcs -s
    ++ egrep -v -e 'Semaphore|key'
    ++ sed '/^$/d'
    ++ awk '{print $2}'
    ++ sort -u
    + for SEMID in '`ipcs -s|egrep -v -e "Semaphore|key"|sed '\''/^$/d'\''|awk '\''{print $2}'\''|sort -u`'
    ++ ipcs -s -i 131074
    ++ tail -2
    ++ head -1
    ++ awk '{print $NF}'
    + PID=13193
    + '[' 13193 -gt 0 ']'
    + ps -p 13193
    + for SEMID in '`ipcs -s|egrep -v -e "Semaphore|key"|sed '\''/^$/d'\''|awk '\''{print $2}'\''|sort -u`'
    ++ ipcs -s -i 196612
    ++ tail -2
    ++ head -1
    ++ awk '{print $NF}'
    + PID=15772
    + '[' 15772 -gt 0 ']'
    + ps -p 15772
    + for SEMID in '`ipcs -s|egrep -v -e "Semaphore|key"|sed '\''/^$/d'\''|awk '\''{print $2}'\''|sort -u`'
    ++ ipcs -s -i 229381
    ++ tail -2
    ++ head -1
    ++ awk '{print $NF}'
    + PID=21180
    + '[' 21180 -gt 0 ']'
    + ps -p 21180
    + set +x

    ReplyDelete
  2. I used the script but its executing till ps -p and ending.... i want to know whether this is functioning as desired.

    ReplyDelete