Killall in the process of the same name

I would like to use killall in a process with the same name from which killall will be executed without killing the process that spawns killall.

So, in more detail, let's say I have a process foo and a process foo is running. I want to be able to run "foo -k" and the new foo kills the old foo without killing itself.

+1


a source to share


3 answers


pgrep foo | grep -v $$ | xargs kill

      

If you don't pgrep

, you'll have to come up with another way to generate a list of PIDs of interest. Some parameters:

  • Use ps with appropriate options followed by some combination of grep, sed and / or awk to match processes and extract PIDs.

  • killall

    can send signal 0 instead SIGTERM

    ; the standard semantics for this is that it does not send a signal, but simply determines whether the process is alive or not. Perhaps you can use killall to select a list of processes and get it to print PIDs from compatible ones that are alive. This will also probably require a bit of post-processing with sed.

  • Perhaps something akin to a Linux filesystem /proc

    with pseudo files storing system data that you could nip. Again, grep / awk / sed are your friends here.



If you really need specific information on how to do this, please comment or email me and I will try to expand on some of these options in more detail.

[Edit: Added additional options for those without pgrep.]

+4


a source


This seems to work with OS X:

killall -s foo | perl -ne 'system $_ unless /\b'$PPID'\b/'

      



killall -s lists what it will do, one PID at a time. Do what he would do other than kill himself.

+2


a source


The usual way to solve this problem is to foo

write your process ID to a file, say something like /var/run/foo.pid

when it starts up in daemon mode. Then you can have a non-daemon version to read the PID from the PID file and call directly kill(2)

. Usually it's like apache and the like. Of course newer OSX daemons go through launchd(8)

instead, but there are a few more that use good old fashioned signals.

+1


a source







All Articles