Delete all SYSTEM V shared memory and semaphores on UNIX-like systems

110,237

Solution 1

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

Solution 2

This works on my Mac OS:

for n in `ipcs -b -m | egrep ^m | awk '{ print $2; }'`; do ipcrm -m $n; done

Solution 3

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.

Solution 4

#!/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

Solution 5

I don't know how to delete all at once, but you can use ipcs to list resources, and then use loop and delete with ipcrm. This should work, but it needs a little work. I remember that I made it work once in class.

Share:
110,237
simone
Author by

simone

Updated on March 12, 2020

Comments

  • simone
    simone about 4 years

    How can I delete all not used semaphores and shared memory with a single command on a UNIX-like system, e.g., Ubuntu?

  • simone
    simone over 14 years
    no this doesn't help me...i'm just doing a simple c project for a fake nfs...i know what are semaphores and shared memory...i just want to do some test on my code and i need to remove all the shared data in one click
  • t0mm13b
    t0mm13b over 14 years
    @simone: You should have stated 'fake nfs' on your original question and pointed out that you understood semaphores and shared memory... it is still not clear as to what is "shared data"?
  • t0mm13b
    t0mm13b over 14 years
    And also inclusion of code as well to show your homework for us SO'ers to see....that would be of help also...
  • Duck
    Duck over 14 years
    Do we know if he is using SysV ipc and not POSIX?
  • MBO
    MBO about 14 years
    @Duck We know he's using Ubuntu, and I checked those commands on Ubuntu
  • Duck
    Duck about 14 years
    Right but ipcs only works with SysV ipc objects. If he is using the posix versions they will show in /dev/shm where he can just rm them.
  • Burkhard
    Burkhard over 13 years
    Thanks. Exactly what I was looking for.
  • Ben
    Ben over 10 years
    Doesn't that delete all of them, even if nattach is nonzero? What about this?: ipcs -m | egrep "0x[0-9a-f]+ [0-9]+"|awk '{ if ($6 == 0) { print $2; } }'|xargs -n1 ipcrm -m
  • user1202136
    user1202136 almost 10 years
    I would replace the for loops with xargs: echo $IPCS_S | xargs -n1 ipcrm -s.
  • brainydexter
    brainydexter almost 10 years
    For me the above regex didn't work. The following did IPCS_Q=ipcs -q | egrep -i "0x[0-9a-f]+.*[0-9]+" | grep $ME | cut -f2 -d" "``
  • LeoPucciBr
    LeoPucciBr over 7 years
    On AIx systems does not work either.. some minor tweaks on the commands did the trick! ipcs -s |grep $ME | awk '{print $2}' The awk command solves the positioning problems.
  • Oleg Oleg
    Oleg Oleg about 5 years
    It's a great answer, it helped me to remove queues with key 0x0 (which cannot be removed using ipcrm -Q option)