Date: Thu, 22 Nov 2007 19:17:09 GMT From: Jan Schaumann <jschauma@netmeister.org> To: freebsd-gnats-submit@FreeBSD.org Subject: misc/118205: new options -r to pkill(1) a pid after timeout Message-ID: <200711221917.lAMJH9uV031214@www.freebsd.org> Resent-Message-ID: <200711221920.lAMJK1OB083967@freefall.freebsd.org>
next in thread | raw e-mail | index | archive | help
>Number: 118205 >Category: misc >Synopsis: new options -r to pkill(1) a pid after timeout >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Thu Nov 22 19:20:00 UTC 2007 >Closed-Date: >Last-Modified: >Originator: Jan Schaumann >Release: >Organization: >Environment: >Description: I occasionally have the need to kill a process iff it has been running for longer than N seconds. Instead of parsing the output of ps(1) and then calling kill(1) myself, I have attached a patch to add the "-r runtime" flag to pkill(1). See http://mail-index.netbsd.org/tech-userlevel/2007/07/26/0003.html for a discussion about this feature in NetBSD. >How-To-Repeat: >Fix: Patch attached with submission follows: diff -bur pkill.orig/pkill.1 pkill/pkill.1 --- pkill.orig/pkill.1 2006-12-20 03:57:22.000000000 -0800 +++ pkill/pkill.1 2007-11-22 11:10:15.000000000 -0800 @@ -50,6 +50,7 @@ .Op Fl M Ar core .Op Fl N Ar system .Op Fl P Ar ppid +.Op Fl r Ar runtime .Op Fl U Ar uid .Op Fl d Ar delim .Op Fl g Ar pgrp @@ -66,6 +67,7 @@ .Op Fl M Ar core .Op Fl N Ar system .Op Fl P Ar ppid +.Op Fl r Ar runtime .Op Fl U Ar uid .Op Fl g Ar pgrp .Op Fl j Ar jid @@ -116,6 +118,9 @@ Restrict matches to processes with a parent process ID in the comma-separated list .Ar ppid . +.It Fl r Ar runtime +Restrict matches to processes with a running time (ie wall clock) larger than +.Ar runtime . .It Fl S Search also in system processes (kernel threads). .It Fl U Ar uid diff -bur pkill.orig/pkill.c pkill/pkill.c --- pkill.orig/pkill.c 2006-11-23 03:55:17.000000000 -0800 +++ pkill/pkill.c 2007-11-22 11:13:03.000000000 -0800 @@ -106,6 +106,7 @@ static int interactive; static int inverse; static int longfmt; +static int runtime; static int matchargs; static int fullmatch; static int kthreads; @@ -180,7 +181,7 @@ pidfilelock = 0; execf = coref = _PATH_DEVNULL; - while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:d:fg:ij:lnos:t:u:vx")) != -1) + while ((ch = getopt(argc, argv, "DF:G:ILM:N:P:SU:d:fg:ij:lnor:s:t:u:vx")) != -1) switch (ch) { case 'D': debug_opt++; @@ -252,6 +253,14 @@ oldest = 1; criteria = 1; break; + case 'r': + runtime = strtol(optarg, &q, 10); + if (*q) { + errx(EXIT_FAILURE, "illegal runtime: %s", optarg); + /* NOTREACHED */ + } + criteria = 1; + break; case 's': makelist(&sidlist, LT_SID, optarg); criteria = 1; @@ -364,6 +373,14 @@ selected[i] = 1; } else selected[i] = 1; + + if (runtime) { + if (kp->p_ustart_sec < (time(NULL) - runtime)) { + selected[i] = 1; + } else { + selected[i] = 0; + } + } } else if (rv != REG_NOMATCH) { regerror(rv, ®, buf, sizeof(buf)); errx(STATUS_ERROR, @@ -464,9 +481,15 @@ continue; } - if (argc == 0) + if (argc == 0) { + if (runtime) { + if (kp->p_ustart_sec < (time(NULL) - runtime)) { selected[i] = 1; } + } else + selected[i] = 1; + } + } if (newest || oldest) { best_tval.tv_sec = 0; @@ -528,9 +551,9 @@ fprintf(stderr, "usage: %s %s [-F pidfile] [-G gid] [-M core] [-N system]\n" - " [-P ppid] [-U uid] [-g pgrp] [-j jid] [-s sid]\n" - " [-t tty] [-u euid] pattern ...\n", getprogname(), - ustr); + " [-P ppid] [-U uid] [-g pgrp] [-j jid] [-r runtime]\n" + " [-s sid] [-t tty] [-u euid] pattern ...\n", + getprogname(), ustr); exit(STATUS_BADUSAGE); } >Release-Note: >Audit-Trail: >Unformatted:
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200711221917.lAMJH9uV031214>