From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 1020c6,bedbbee8b27b663b X-Google-Attributes: gid1020c6,public X-Google-Thread: fee84,bedbbee8b27b663b X-Google-Attributes: gidfee84,public From: pdf@morgan.ucs.mun.ca (Paul David Fardy) Subject: Re: killing a process by name? Date: 1999/06/09 Message-ID: <7jln65$ss1$1@coranto.ucs.mun.ca>#1/1 X-Deja-AN: 487472115 References: <375d6a41.14695581@news.aurora.edu> <375DBA80.76E3B5FA@fulcrum.com.au> Organization: Memorial University of Newfoundland Newsgroups: comp.unix.osf.osf1,comp.unix.admin Christopher Kolar wrote: >> Hello everyone. This is probably a basic question, but I have not >> found the solution yet so I thought I would float it. I have a host >> running OSF 4.0d. After everyone leaves for the day I am going to >> have cron start a program "foo". Foo should run until midnight, when >> it needs to be killed so that out nightly backups can happen. Mark Lambert writes: >Serguei Patchkovskii had a more elegant awk solution, but mine is (I think) >more understandable for non-awk guru's... >the brute force approach :0 >try >kill `ps -ef | grep foo | grep -v grep | awk '{print $2}'` Danger! Will Robinson! Danger! :-) Is this cron job to run as root? What if I'm running an important job? /local/sbin/food -for the-hungry Will they not starve? One should ensure not "foo", but the complete path (e.g. "/local/sbin/foo") appears in the process table. Then one should test for equality, using something like the awk solution provided by Serguei. You could also use 0 0 * * * kill `/sbin/init.d/bin/getpid /local/sbin/foo` The getpid script is used by the init.d (rc2.d and rc3.d) scripts when the relevant daemons aren't using /var/run. It provides a variant of Serguei's solution. One still might worry about multiple instances... Perhaps it would be better to wrap your program with a script like: #!/bin/sh echo $$ > /var/run/foo.pid exec /local/sbin/foo and use 0 0 * * * kill `cat /var/run/foo.pid` though this will fail if foo forks a new process and exits. Paul Fardy -- Paul David Fardy | pdf@morgan.ucs.mun.ca Computing and Communications | pdf@InfoNET.st-johns.nf.ca Memorial University of Newfoundland | St. John's, NF A1C 5S7 |