tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

patch: new option for pkill/pgrep



I originally tried to contact Andrew Doran, but it looks like 
ad%netbsd.org@localhost
no longer works ?

Over in OpenBSD-land, I implemented and committed a new option for pgrep:
-q, that matches what grep does (silent option).

Here's the OpenBSD patch, that may need adapting for NetBSD.

Since the original code comes from NetBSD, the least courtesy I could do was
share that addition with you. ;-)

Cheers.

Index: pkill.1
===================================================================
RCS file: /cvs/src/usr.bin/pkill/pkill.1,v
retrieving revision 1.18
retrieving revision 1.20
diff -u -p -u -r1.18 -r1.20
--- pkill.1     9 Feb 2012 20:04:35 -0000       1.18
+++ pkill.1     21 Aug 2012 09:45:50 -0000      1.20
@@ -36,7 +36,7 @@
 .Nd find or signal processes by name
 .Sh SYNOPSIS
 .Nm pgrep
-.Op Fl flnovx
+.Op Fl flnoqvx
 .Op Fl d Ar delim
 .Op Fl G Ar gid
 .Op Fl g Ar pgrp
@@ -49,7 +49,7 @@
 .Op Ar pattern ...
 .Nm pkill
 .Op Fl Ar signal
-.Op Fl flnovx
+.Op Fl flnoqvx
 .Op Fl G Ar gid
 .Op Fl g Ar pgrp
 .Op Fl P Ar ppid
@@ -116,6 +116,13 @@ Cannot be used in conjunction with
 Restrict matches to processes with a parent process ID in the
 comma-separated list
 .Ar ppid .
+.It Fl q
+Quiet mode.
+Perform the action, but don't display anything on standard output.
+Note that
+.Fl q
+takes precedence over other display options such as
+.Fl l .
 .It Fl s Ar sid
 Restrict matches to processes with a session ID in the comma-separated
 list
Index: pkill.c
===================================================================
RCS file: /cvs/src/usr.bin/pkill/pkill.c,v
retrieving revision 1.28
retrieving revision 1.30
diff -u -p -u -r1.28 -r1.30
--- pkill.c     10 Jul 2012 12:48:08 -0000      1.28
+++ pkill.c     21 Aug 2012 10:32:38 -0000      1.30
@@ -83,6 +83,7 @@ int   pgrep;
 int    signum = SIGTERM;
 int    newest;
 int    oldest;
+int    quiet;
 int    inverse;
 int    longfmt;
 int    matchargs;
@@ -152,7 +153,7 @@ main(int argc, char **argv)
 
        criteria = 0;
 
-       while ((ch = getopt(argc, argv, "G:P:T:U:d:fg:lnos:t:u:vx")) != -1)
+       while ((ch = getopt(argc, argv, "G:P:T:U:d:fg:lnoqs:t:u:vx")) != -1)
                switch (ch) {
                case 'G':
                        makelist(&rgidlist, LT_GROUP, optarg);
@@ -193,6 +194,9 @@ main(int argc, char **argv)
                        oldest = 1;
                        criteria = 1;
                        break;
+               case 'q':
+                       quiet = 1;
+                       break;
                case 's':
                        makelist(&sidlist, LT_SID, optarg);
                        criteria = 1;
@@ -420,7 +424,7 @@ main(int argc, char **argv)
                else if (rv != STATUS_ERROR)
                        rv = STATUS_MATCH;
        }
-       if (pgrep && j)
+       if (pgrep && j && !quiet)
                putchar('\n');
 
        exit(rv);
@@ -432,9 +436,9 @@ usage(void)
        const char *ustr;
 
        if (pgrep)
-               ustr = "[-flnovx] [-d delim]";
+               ustr = "[-flnoqvx] [-d delim]";
        else
-               ustr = "[-signal] [-flnovx]";
+               ustr = "[-signal] [-flnoqvx]";
 
        fprintf(stderr, "usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid]"
            "\n\t[-T rtable] [-t tty] [-U uid] [-u euid] [pattern ...]\n",
@@ -446,7 +450,7 @@ usage(void)
 int
 killact(struct kinfo_proc *kp, int dummy)
 {
-       if (longfmt)
+       if (longfmt && !quiet)
                printf("%d %s\n", (int)kp->p_pid, kp->p_comm);
 
        if (kill(kp->p_pid, signum) == -1 && errno != ESRCH) {
@@ -461,6 +465,8 @@ grepact(struct kinfo_proc *kp, int print
 {
        char **argv;
 
+       if (quiet)
+               return (0);
        if (printdelim)
                fputs(delim, stdout);
        if (longfmt && matchargs) {

----- End forwarded message -----


Home | Main Index | Thread Index | Old Index