Well, I've just found out that with extensive usage of pidof
in very short intervals, the seemingly tiny tool can be a great CPU hog. (Source: top
)
On my older machine, it can easily reach 30 percent peaks especially in batch usage, though only for a short time, but I think that for a simple task like finding the PID of a process, the footprint of such tool should be one fifth of pidof
's (if at all).
That is also why I wonder if it might be more sensible to "construct" the finding of the process ID with built-in standard tools. It would not be surprising to me if the sum of CPU load caused by executing the whole pipe managed to stay below the load caused by running pidof
standalone.
Furthermore, it would be interesting to know what is causing these high peaks. Maybe there is even somebody here amongst you guys who has dug a little deeper into the pidof
code? 🙂
Best Answer
Here are some examples to find pids of processes named "apache2". There are minor differences (newlines in output) between these and
pidof
but otherwise ought to work the same, in pipelines, etc.Using
pidof
:Newline-separated:
Space-separated:
I did some silly timings of the above commands.
pidof: 7,412,110 nanoseconds
ps | grep | awk : 16,682,606 nanoseconds
On my machine,
pidof
is loads faster (as expected).The output of
pidof
seems to be sorted reverse, so this would be my preferred alternative incantation: