pidof: allow to suppress output

Often pidof is used in shell scripts in this form:

    if pidof daemon >/dev/null; then
        ...
    fi

The redirection to /dev/null is needed because otherwise the script
would output the found PIDs.
Let's add a -q option which, similary to grep, just sets the exit code.
Also exit on first match, as there is no reason to proceed further when
at least a process is matched.

Tested with:

    $ ./pidof bash
    17701 14019 5276 2967
    $ echo $?
    0
    $ ./pidof bashx
    $ echo $?
    1
    $ ./pidof -q bash
    $ echo $?
    0
    $ ./pidof -q bashx
    $ echo $?
    1
This commit is contained in:
Matteo Croce
2019-03-19 14:33:01 +01:00
committed by Craig Small
parent 79097e55e4
commit b067ecc886
2 changed files with 24 additions and 15 deletions

14
pidof.1
View File

@ -15,17 +15,17 @@
.\" along with this program; if not, write to the Free Software
.\" Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
.\"
.TH PIDOF 1 "2020-06-04" "" "User Commands"
.TH PIDOF 1 "2020-12-22" "" "User Commands"
.SH NAME
pidof -- find the process ID of a running program
.SH SYNOPSIS
.B pidof
.RB [ \-s ]
.RB [ \-c ]
.RB [ \-q ]
.RB [ \-w ]
.RB [ \-x ]
.RB [ \-o
.IR omitpid[,omitpid...] ]
.RB [ \-o
.IR omitpid[,omitpid...]... ]
.RB [ \-S
.IR separator ]
@ -42,12 +42,14 @@ Single shot - this instructs the program to only return one \fIpid\fP.
Only return process ids that are running with the same root directory.
This option is ignored for non-root users, as they will be unable to check
the current root directory of processes they do not own.
.IP \-x
Scripts too - this causes the program to also return process id's of
shells running the named scripts.
.IP \-q
Quiet mode, suppress any output and only sets the exit status accordingly.
.IP \-w
Show also processes that do not have visible command line (e.g. kernel
worker threads).
.IP \-x
Scripts too - this causes the program to also return process id's of
shells running the named scripts.
.IP "-o \fIomitpid\fP"
Tells \fIpidof\fP to omit processes with that process id. The special
pid \fB%PPID\fP can be used to name the parent process of the \fIpidof\fP