home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / shell / 3716 < prev    next >
Encoding:
Text File  |  1992-08-31  |  3.6 KB  |  154 lines

  1. Newsgroups: comp.unix.shell
  2. Path: sparky!uunet!mcsun!sunic!aun.uninett.no!ugle.unit.no!news
  3. From: Harald.Eikrem@delab.sintef.no
  4. Subject: Re: Need Script To Kill Process Using Substring Of Application Name
  5. In-Reply-To: shah@fnsony.fnal.gov's message of 31 Aug 92 17:21:44 GMT
  6. Message-ID: <1992Aug31.222346*Harald.Eikrem@delab.sintef.no>
  7. Followup-To: comp.unix.shell
  8. Sender: news@ugle.unit.no (NetNews Administrator)
  9. Organization: SINTEF DELAB, Trondheim, Norway.
  10. References: <x6gnwvc.westes@netcom.com> <bvh1H-l9b9@atlantis.psu.edu>
  11.     <MONTNARO.92Aug30221640@ausable.crd.ge.com> <rqhnklc.tunxis@netcom.com>
  12.     <1992Aug31.095604.488@dlpinc00.rn.com> <1992Aug31.113416.8536@panix.com>
  13.     <2238@fnnews.fnal.gov>
  14. Date: 31 Aug 92 22:23:46
  15. Lines: 137
  16.  
  17. This is where I find a simple C program much preferrable over those
  18. kill-me-all scripts.  This is as portable as `ps' itself (:.  It doesn't
  19. do regexp's, however.  No man page, either.
  20.  
  21. To find processes by substrings (this is for a BSD style `ps'):
  22.  
  23.     % psf map mail
  24.       PID TT STAT  TIME COMMAND
  25.        57 ?  S     0:20 portmap
  26.       125 ?  IW    0:05 -Waiting for connection (sendmail)
  27.       154 ?  S     1:58 /local/etc/amd -l /usr/tmp/amd-log /net map.net /home map.home
  28.  
  29. To find all processes belonging to myself (except `psf' itself):
  30.  
  31.     % psf -u $USER
  32.     USER       PID %CPU %MEM   SZ  RSS TT STAT START  TIME COMMAND
  33.     harald   19706 30.8  1.5  168  408 p3 R    22:09   0:00 ps -axwu
  34.     harald   18137  0.0  1.6  368  432 p3 S    20:54   0:09 -tcsh (tcsh)
  35.  
  36. To kill a set of processes:
  37.  
  38.     % psf -pid sendmail | xargs kill
  39.  
  40. The name psf -- "ps filter" -- was chosen a long time ago.  If you dont
  41. like the taste of it, name it something else.....
  42.  
  43.   ~~h
  44.  
  45.  
  46. ===== psf.c =====
  47. #include <stdio.h>
  48. extern    errno;
  49. char*    index();
  50.  
  51. #define BZ 200
  52. #ifdef hpux
  53. # define SYSV
  54. #endif
  55.  
  56. #ifdef SYSV
  57. # define PSCOMMAND "exec ps -ef"
  58. # define index strchr
  59. # define rindex strrchr
  60. #else
  61. # define PSCOMMAND "exec ps -axw"
  62. #endif
  63.  
  64. main(c,v) char** v;
  65. {
  66.     int    i;
  67.     char*    s;
  68.     char*    t;
  69.     char    inp[BZ];
  70.     FILE*    p;
  71.     int    r=0;
  72.     int    l[50];
  73.     char    thisproc[BZ];
  74.     int    thisproc_l;
  75.     static char    ps_command[40] = PSCOMMAND;
  76.     int    pidonly=0;
  77.     char    pids[BZ];
  78.     char    header[BZ];
  79.     int    noheader=0;
  80.     int    matches=0;
  81.  
  82.     if (c<2 || *v[1]==0) goto usage;
  83.     if (*v[1]=='-' && !v[2]) {
  84. usage:        fprintf(stderr,"usage: %s [-<ps options>|-pid] string [string....]\n",*v);
  85.         exit(1);
  86.     }
  87.     if (*v[1]=='-' && v[1][1]) {
  88.         if (v[1][1]=='p' && v[1][2]=='i' && v[1][3]=='d') ++pidonly;
  89.         else {    i = 1;
  90.             if (v[1][1] == 'H') { noheader++; i++; }
  91.             strcat(ps_command,&v[1][i]);
  92.         }
  93.     }
  94.     if (!(p = popen(ps_command, "r"))) {
  95.         perror("popen");
  96.         exit(errno);
  97.     }
  98.     i = 0;
  99.     strcpy(thisproc,v[0]);
  100.     while (v[++i]) {
  101.         l[i] = strlen(v[i]);
  102.         strcat(thisproc," ");
  103.         strcat(thisproc,v[i]);
  104.     }
  105.     thisproc_l = strlen(thisproc);
  106.     *pids = 0;
  107.     *header = 0;
  108.     while (fgets(inp,BZ,p)) {
  109.         ++r;
  110.         if (r==1 && !pidonly && !noheader) {
  111.             strcpy(header,inp);
  112.             continue;
  113.         }
  114.  
  115.         s = inp;
  116.         while (s=index(s,*thisproc)) {
  117.             if (strncmp(s,thisproc,thisproc_l)==0) goto next;
  118.             ++s;
  119.         }
  120.         i = (*v[1]=='-');
  121.         while (v[++i]) {
  122.             s = inp;
  123.             while (s=index(s,*v[i])) {
  124.                 if (strncmp(s,v[i],l[i])==0) {
  125.                     if (*header) {
  126.                         fputs(header,stdout);
  127.                         *header = 0;
  128.                     }
  129.                     ++matches;
  130.                     if (pidonly) {
  131.                         s = inp;
  132. #ifdef SYSV
  133.                         while (*s==' ') ++s;
  134.                         while (*s && *s!=' ') ++s;
  135. #endif SYSV
  136.                         while (*s==' ') ++s;
  137.                         t = s;
  138.                         while (*t && *t!=' ') ++t;
  139.                         *t = 0;
  140.                         if (*pids) strcat(pids," ");
  141.                         strcat(pids,s);
  142.                     } else
  143.                         fputs(inp,stdout);
  144.                     goto next;
  145.                 }
  146.                 ++s;
  147.             }
  148.         }
  149. next:;    }
  150.     pclose(p);
  151.     if (*pids) puts(pids);
  152.     exit(matches==0);
  153. }
  154.