![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread |
Rating:
|
Display Modes |
|
||||
|
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.... |
|
||||
|
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 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.. |
|
|||||
|
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 |
|
||||
|
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 |
|
||||
|
(But don't use kill -9 if you don't know exactly what you are doing.)
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| find process id, kill |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|