The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Diff b/n kill and kill -9 ammu UNIX for Advanced & Expert Users 2 07-18-2007 05:29 PM
kill(0,-9) don't kill the process umen High Level Programming 9 06-19-2007 06:09 AM
not able to kill find with kill -9 Amardeep UNIX for Dummies Questions & Answers 5 01-04-2007 05:49 PM
KILL PID, intern should kill another PID. rkrgarlapati Shell Programming and Scripting 4 10-17-2006 07:47 AM
When kill doesnt work, how to kill a process ? VijayHegde UNIX for Advanced & Expert Users 3 05-12-2006 04:24 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 2.00 average. Display Modes
  #1 (permalink)  
Old 08-02-2008
luckypower luckypower is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
KILL without PID

Hellow Experts

i have one problem.
i run one script in backgroun.
and i want to kill that script with only script name.....
so what's the solution..

for your info
my script name is "testscript" n it contains "sleep 100"

thanks....
  #2 (permalink)  
Old 08-02-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
If your system has pidof, use that. Otherwise, the customary solution is to run grep on a ps listing of your processes, and use that to find the PID to pass to kill. However, a naive attempt will have the problem that it will find itself in the process listing, and commit suicide instead of kill the intended target. The proper workaround for that is to use a regular expression which does not directly match itself as the search string.

Unfortunately, the options and output format of ps varies from one system to another. The following works for me on a recent version of Ubuntu.

Code:
ps t | awk '$5 ~ /^[t]estscript/ { print $1 }' | xargs -r kill
The use of [t] instead of just a plain t is the workaround for the "script will kill itself" problem. The use of xargs -r prevents kill from being run at all if there are no matches (there would only be a warning message about running kill with no arguments, so that is not a very fatal problem).

The option t and the field numbers $1 and $5 might need to be changed for your system. If you google for a similar solution for your particular platform, look out for the problems outlined above. For stylistic reasons, a single awk script should be preferred over what is affectionately called Useless Use of Grep.

Last edited by era; 08-02-2008 at 03:44 AM..
  #3 (permalink)  
Old 08-02-2008
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
  
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 6,522
If you are using Linux, you can use the killall command to kill a process using the process name(s) (not the PID).

See, for example: killall(1): kill processes by name - Linux man page

Se all, pkill: http://linux.die.net/man/1/pkill
  #4 (permalink)  
Old 08-02-2008
luckypower luckypower is offline
Registered User
  
 

Join Date: Aug 2008
Posts: 6
I got the solution
Use
kill `ps -C test.sh -o pid=`
  #5 (permalink)  
Old 08-02-2008
Neo's Avatar
Neo Neo is online now Forum Staff  
Administrator
  
 

Join Date: Sep 2000
Location: Asia Pacific
Posts: 6,522
On Linux, you could simple type:

killall testscript
  #6 (permalink)  
Old 08-02-2008
redoubtable redoubtable is offline
Registered User
  
 

Join Date: Aug 2008
Location: Portugal
Posts: 242
Something simple:
killall procname

Another way:
inside your script, create a temporary file that holds the current pid of your script. When you wish to terminate the process, use that as such: kill -9 `cat pidfile`

If you want something very specific and accurate:
- setuid() to another user which only runs that process (script)
- pkill -9 -u youruser
You can also use pkill to kill all processes using a certain terminal pkill -9 -t pts/1
  #7 (permalink)  
Old 08-03-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
(But don't use kill -9 if you don't know exactly what you are doing.)
Sponsored Links
Closed Thread

Bookmarks

Tags
find process id, kill

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 08:56 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0