How can I delete all not used semaphores and shared memory with a single command on a UNIX-like system, e.g., Ubuntu?
#!/bin/bash
ipcs -m | grep `whoami` | awk '{ print $2 }' | xargs -n1 ipcrm -m
ipcs -s | grep `whoami` | awk '{ print $2 }' | xargs -n1 ipcrm -s
ipcs -q | grep `whoami` | awk '{ print $2 }' | xargs -n1 ipcrm -q
ipcs -s | grep $USERNAME | perl -e 'while (<STDIN>) { @a=split(/\s+/); print `ipcrm sem $a[1]`}'
or
ipcs -s | grep $USERNAME | awk ' { print $2 } ' | xargs ipcrm sem
Change $USERNAME to a real username.
Check if there is anything to delete with :
ipcs -a | grep `whoami`
On linux delete them all via :
ipcs | nawk -v u=`whoami` '/Shared/,/^$/{ if($6==0&&$3==u) print "ipcrm shm",$2,";"}/Semaphore/,/^$/{ if($3==u) print "ipcrm sem",$2,";"}' | /bin/sh
For sun it would be :
ipcs -a | nawk -v u=`whoami` '$5==u &&(($1=="m" && $9==0)||($1=="s")){print "ipcrm -"$1,$2,";"}' | /bin/sh
courtsesy of di.uoa.gr
Check again if all is ok
For deleting your sems/shared mem - supposing you are a user in a workstation with no admin rights
Here, save and try this script (kill_ipcs.sh) on your shell:
#!/bin/bash
ME=`whoami`
IPCS_S=`ipcs -s | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "`
IPCS_M=`ipcs -m | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "`
IPCS_Q=`ipcs -q | egrep "0x[0-9a-f]+ [0-9]+" | grep $ME | cut -f2 -d" "`
for id in $IPCS_M; do
ipcrm -m $id;
done
for id in $IPCS_S; do
ipcrm -s $id;
done
for id in $IPCS_Q; do
ipcrm -q $id;
done
We use it whenever we run IPCS programs in the university student server. Some people don't always cleanup so...it's needed :P
1 line will do all
For message queue
ipcs -q | sed "$ d; 1,2d" | awk '{ print "Removing " $2; system("ipcrm -q " $2) }'
ipcs -q
will give the records of message queues
sed "$ d; 1,2d "
will remove last blank line ("$ d"
) and first two header lines ("1,2d"
)
awk
will do the rest i.e. printing and removing using command "ipcrm -q"
w.r.t. the value of column 2 (coz $2
)