home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.shell
- Path: sparky!uunet!mcsun!sunic!aun.uninett.no!ugle.unit.no!news
- From: Harald.Eikrem@delab.sintef.no
- Subject: Re: Need Script To Kill Process Using Substring Of Application Name
- In-Reply-To: shah@fnsony.fnal.gov's message of 31 Aug 92 17:21:44 GMT
- Message-ID: <1992Aug31.222346*Harald.Eikrem@delab.sintef.no>
- Followup-To: comp.unix.shell
- Sender: news@ugle.unit.no (NetNews Administrator)
- Organization: SINTEF DELAB, Trondheim, Norway.
- References: <x6gnwvc.westes@netcom.com> <bvh1H-l9b9@atlantis.psu.edu>
- <MONTNARO.92Aug30221640@ausable.crd.ge.com> <rqhnklc.tunxis@netcom.com>
- <1992Aug31.095604.488@dlpinc00.rn.com> <1992Aug31.113416.8536@panix.com>
- <2238@fnnews.fnal.gov>
- Date: 31 Aug 92 22:23:46
- Lines: 137
-
- This is where I find a simple C program much preferrable over those
- kill-me-all scripts. This is as portable as `ps' itself (:. It doesn't
- do regexp's, however. No man page, either.
-
- To find processes by substrings (this is for a BSD style `ps'):
-
- % psf map mail
- PID TT STAT TIME COMMAND
- 57 ? S 0:20 portmap
- 125 ? IW 0:05 -Waiting for connection (sendmail)
- 154 ? S 1:58 /local/etc/amd -l /usr/tmp/amd-log /net map.net /home map.home
-
- To find all processes belonging to myself (except `psf' itself):
-
- % psf -u $USER
- USER PID %CPU %MEM SZ RSS TT STAT START TIME COMMAND
- harald 19706 30.8 1.5 168 408 p3 R 22:09 0:00 ps -axwu
- harald 18137 0.0 1.6 368 432 p3 S 20:54 0:09 -tcsh (tcsh)
-
- To kill a set of processes:
-
- % psf -pid sendmail | xargs kill
-
- The name psf -- "ps filter" -- was chosen a long time ago. If you dont
- like the taste of it, name it something else.....
-
- ~~h
-
-
- ===== psf.c =====
- #include <stdio.h>
- extern errno;
- char* index();
-
- #define BZ 200
- #ifdef hpux
- # define SYSV
- #endif
-
- #ifdef SYSV
- # define PSCOMMAND "exec ps -ef"
- # define index strchr
- # define rindex strrchr
- #else
- # define PSCOMMAND "exec ps -axw"
- #endif
-
- main(c,v) char** v;
- {
- int i;
- char* s;
- char* t;
- char inp[BZ];
- FILE* p;
- int r=0;
- int l[50];
- char thisproc[BZ];
- int thisproc_l;
- static char ps_command[40] = PSCOMMAND;
- int pidonly=0;
- char pids[BZ];
- char header[BZ];
- int noheader=0;
- int matches=0;
-
- if (c<2 || *v[1]==0) goto usage;
- if (*v[1]=='-' && !v[2]) {
- usage: fprintf(stderr,"usage: %s [-<ps options>|-pid] string [string....]\n",*v);
- exit(1);
- }
- if (*v[1]=='-' && v[1][1]) {
- if (v[1][1]=='p' && v[1][2]=='i' && v[1][3]=='d') ++pidonly;
- else { i = 1;
- if (v[1][1] == 'H') { noheader++; i++; }
- strcat(ps_command,&v[1][i]);
- }
- }
- if (!(p = popen(ps_command, "r"))) {
- perror("popen");
- exit(errno);
- }
- i = 0;
- strcpy(thisproc,v[0]);
- while (v[++i]) {
- l[i] = strlen(v[i]);
- strcat(thisproc," ");
- strcat(thisproc,v[i]);
- }
- thisproc_l = strlen(thisproc);
- *pids = 0;
- *header = 0;
- while (fgets(inp,BZ,p)) {
- ++r;
- if (r==1 && !pidonly && !noheader) {
- strcpy(header,inp);
- continue;
- }
-
- s = inp;
- while (s=index(s,*thisproc)) {
- if (strncmp(s,thisproc,thisproc_l)==0) goto next;
- ++s;
- }
- i = (*v[1]=='-');
- while (v[++i]) {
- s = inp;
- while (s=index(s,*v[i])) {
- if (strncmp(s,v[i],l[i])==0) {
- if (*header) {
- fputs(header,stdout);
- *header = 0;
- }
- ++matches;
- if (pidonly) {
- s = inp;
- #ifdef SYSV
- while (*s==' ') ++s;
- while (*s && *s!=' ') ++s;
- #endif SYSV
- while (*s==' ') ++s;
- t = s;
- while (*t && *t!=' ') ++t;
- *t = 0;
- if (*pids) strcat(pids," ");
- strcat(pids,s);
- } else
- fputs(inp,stdout);
- goto next;
- }
- ++s;
- }
- }
- next:; }
- pclose(p);
- if (*pids) puts(pids);
- exit(matches==0);
- }
-