Why does pidof from homebrew returns always zero

homebrew

I've installed pidof via homebrew

brew install pidof

And think about two cases:

$ pidof bash > /dev/null; echo $?
$ pidof asdf > /dev/null; echo $?

The results are:

0
0

Why does it happen? Other linux throw 1 when pidof couldn't find process.

Best Answer

The reason is that the pidof from HomeBrew is a different program than pidof in Linux. Even though they share the same name, and parts of the same functionality, they're actually quite different.

For example the HomeBrew pidof will also kill processes for you when specifying the -k argument, which Linux pidof cannot do. In contrast Linux pidof lets you specify you only want a single PID returned in single-shot mode with the -s parameter, the HomeBrew version hasn't got such an option. There are many other differences.

On Linux, pidof comes from the ubiquitous sysvinit-tools package. When you do a pidof programname in Linux, it loops through the running processes while keeping track of whether or not it found the program name you're looking for. If it doesn't find it, it returns an exit status of !0 (which is 1).

In HomeBrew, pidof is a standalone utility from "Night Productions". Their program has no source in common with the Linux pidof, and does not track whether or not it found the program name. It always returns an exit status of 0 in any case.