3

I have 2 processes with the same name, but different PIDs. I need to find out the process with the lowest PID among these 2 and kill it. How do i do that?

Ava
  • 31
  • 1

2 Answers2

7

A bit contrived, but this does the trick (using bash as an example):

pidof bash | grep -o "[0-9]*" | sort -n | sed '1q'

or

pidof bash | tr -s " " "\n" | sort -n | sed '1q'

keep in mind that the "lowest PID" doesn't really mean anything with regard to startup order unless you haven't had enough processes to wrap around from the max down to the low unused numbers again. A better (and probably more-complex) way of doing this would be to kill either the older process or the newer process, depending on which one is bad.

You can find some inspiration here How do you kill all Linux processes that are older than a certain age?

Community
  • 1
  • 1
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
0

Unix, or a *nix with a /proc directory?

If you have /proc support, parse through /proc/[0-9]+/cmdline to look for the processes whose command matches what you're looking for; the directory name (after /proc) is the id.

opendir() and readdir() will be your tools to parse through the directory.

If you don't have /proc support, you can popen("ps -options here", "r"); to read the output of ps (with whatever options are appropriate for your system) to parse through the process list.

mah
  • 39,056
  • 9
  • 76
  • 93