Some months ago there was an email about adding a 'i' option
to pgrep/pkill, one suggestion from that discussion was giving
pkill the ability to say if nothing had been killed.

http://marc.info/?l=openbsd-tech&m=130789344027691&w=2

I must say, I liked this idea. But only got time recently
to have a look. Anyway FreeBSD has the code. Here it is
imported into OpenBSD's pgrep/pkill. The diff does 
slightly more than just inform you of no kills, but I
like that aspect as well.

Comments/oks?

-lum

Index: pkill.1
===================================================================
RCS file: /cvs/src/usr.bin/pkill/pkill.1,v
retrieving revision 1.16
diff -u -p -r1.16 pkill.1
--- pkill.1     29 Sep 2010 07:44:56 -0000      1.16
+++ pkill.1     15 Jan 2012 18:03:00 -0000
@@ -48,7 +48,7 @@
 .Op Ar pattern ...
 .Nm pkill
 .Op Fl Ar signal
-.Op Fl fnovx
+.Op Fl flnovx
 .Op Fl G Ar gid
 .Op Fl g Ar pgrp
 .Op Fl P Ar ppid
@@ -100,9 +100,9 @@ process.
 If used in conjunction with
 .Fl f ,
 print the process ID and the full argument list for each matching process.
-This option can only be used with the
-.Nm pgrep
-command.
+For
+.Nm pkill ,
+display the kill command used for each process killed.
 .It Fl n
 Match only the most recently created (newest) process, if any.
 Cannot be used in conjunction with
Index: pkill.c
===================================================================
RCS file: /cvs/src/usr.bin/pkill/pkill.c,v
retrieving revision 1.19
diff -u -p -r1.19 pkill.c
--- pkill.c     10 Apr 2011 03:20:59 -0000      1.19
+++ pkill.c     15 Jan 2012 18:03:00 -0000
@@ -112,6 +112,7 @@ main(int argc, char **argv)
        char buf[_POSIX2_LINE_MAX], *mstr, **pargv, *p, *q;
        int i, j, ch, bestidx, rv, criteria;
        int (*action)(struct kinfo_proc *, int);
+       int did_action;
        struct kinfo_proc *kp;
        struct list *li;
        u_int32_t bestsec, bestusec;
@@ -176,8 +177,6 @@ main(int argc, char **argv)
                        criteria = 1;
                        break;
                case 'l':
-                       if (!pgrep)
-                               usage();
                        longfmt = 1;
                        break;
                case 'n':
@@ -392,11 +391,16 @@ main(int argc, char **argv)
        /*
         * Take the appropriate action for each matched process, if any.
         */
+       did_action = 0;
        rv = STATUS_NOMATCH;
        for (i = 0, j = 0, kp = plist; i < nproc; i++, kp++) {
                if ((kp->p_flag & P_SYSTEM) != 0 || kp->p_pid == mypid)
                        continue;
                if (selected[i]) {
+                       if (longfmt && !pgrep) {
+                               did_action = 1;
+                               printf("kill -%d %d\n", signum, (int)kp->p_pid);
+                       }
                        if (inverse)
                                continue;
                } else if (!inverse)
@@ -407,6 +411,9 @@ main(int argc, char **argv)
                else if (rv != STATUS_ERROR)
                        rv = STATUS_MATCH;
        }
+        if (!did_action && !pgrep && longfmt)
+                fprintf(stderr,
+                    "No matching processes belonging to you were found\n");
        if (pgrep && j)
                putchar('\n');
 
@@ -421,7 +428,7 @@ usage(void)
        if (pgrep)
                ustr = "[-flnovx] [-d delim]";
        else
-               ustr = "[-signal] [-fnovx]";
+               ustr = "[-signal] [-flnovx]";
 
        fprintf(stderr, "usage: %s %s [-G gid] [-g pgrp] [-P ppid] [-s sid] "
            "[-t tty]\n\t[-U uid] [-u euid] [pattern ...]\n", __progname, ustr);

Reply via email to