Sponsored Content
Full Discussion: KILL without PID
Top Forums UNIX for Advanced & Expert Users KILL without PID Post 302220914 by era on Saturday 2nd of August 2008 03:32:56 AM
Old 08-02-2008
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 04:44 AM..
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

display went awkwards after issue kill pid

hi, i kill a process which is topas. then i do a fg of the process itself and got this Signal 15 received.finally, the display went as belows.... root@myhost:/]ksh: ^L^L^Lps: not found. root@myhost:/] PID TTY TIME CMD ... (4 Replies)
Discussion started by: yls177
4 Replies

2. Shell Programming and Scripting

pass pid to kill using script

Hi there, i wonder if anyone can help is there any way that i can write a script that will kill all current ftp processes, for example if ps -ef | grep ftp produces 3 active proceses, then I would like to somehow extract the PID for each one and pass that to kill -9 has anybody done this... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

3. Shell Programming and Scripting

KILL PID, intern should kill another PID.

Hi All, In my project i have two process runs in the back end. Once i start my project, and execute the command ps, i get below output: PID TTY TIME CMD 9086 pts/1 0:00 ksh 9241 pts/1 0:02 java 9240 pts/1 0:00 shell_script_bg java with 9241 PID is the main... (4 Replies)
Discussion started by: rkrgarlapati
4 Replies

4. Shell Programming and Scripting

When kill [pid] does not work...

Hi, On my Linux machine, using Bash, I sometimes run into a situation where doing the following does not seem to work at all. kermit@fastbox ~ $ ps -A | grep firefox-bin 5375 ? 00:06:57 firefox-bin <defunct> 5624 ? 00:00:00 firefox-bin kermit@fastbox ~ $ kill 5624... (7 Replies)
Discussion started by: kermit
7 Replies

5. AIX

kill -3 <PID> ... where the output file goes?

Hi all, I am generating the coredump of my JBoss, and by default it puts it in to a particular directory. i.e. JBOSS_HOME/. I would like this output file to be created, lets say in /tmp/dump/. I tried the following: kill -3 9404940>/tmp/dump/out.txt But it created... (3 Replies)
Discussion started by: haroon_a
3 Replies

6. UNIX for Dummies Questions & Answers

How to Kill process with dynamic PID?

Hello, I have problem with killing red5 process running on linux server. As this process is continuously changing its PID so it can't be killed with "kill -9 PID" command. First I used following command to list RED5 process ps aux | grep red5 which showed me root 5832 0.0 0.0 4820 756pts/0... (1 Reply)
Discussion started by: ninadgac
1 Replies

7. Shell Programming and Scripting

How to Kill process with dynamic PID?

Hello, I have problem with killing red5 process running on linux server. As this process is continuously changing its PID so it can't be killed with "kill -9 PID" command. First I used following command to list RED5 process ps aux | grep red5 which showed me root 5832 0.0 0.0 4820 756pts/0... (4 Replies)
Discussion started by: ninadgac
4 Replies

8. Shell Programming and Scripting

Kill a PID using script

Hi, I wrote a script to kill a process id. I am able to kill the PID only if I enter the root password in the middle of the execution because I did not run as root i.e after i run the script from the terminal, instead of killing directly, it is killing only after entering the pass when it... (12 Replies)
Discussion started by: rajkumarme_1
12 Replies

9. Shell Programming and Scripting

Kill PID with one liner

Hello Friends, I've been trying to write a one line which checks java processes and filter them for a user (testuser) and then check process arguments with PARGS command and then check if there is certain patterns exists in pargs output then kill the process. I have tried the following so... (2 Replies)
Discussion started by: EAGL€
2 Replies

10. AIX

Kill pid

I created a program to kill long running pid processes. I am getting the following error message: -f command cannot be found. I also want to count the number of pids that are killed and append the results to a text file. I am new to shell script programming. 1.The first part of code... (10 Replies)
Discussion started by: dellanicholson
10 Replies
KILL(1) 							   User Commands							   KILL(1)

NAME
kill - send signals to processes, or list signals SYNOPSIS
kill [-s SIGNAL | -SIGNAL] PID... kill -l [SIGNAL]... kill -t [SIGNAL]... DESCRIPTION
Send signals to processes, or list signals. Mandatory arguments to long options are mandatory for short options too. -s, --signal=SIGNAL, -SIGNAL specify the name or number of the signal to be sent -l, --list list signal names, or convert signal names to/from numbers -t, --table print a table of signal information --help display this help and exit --version output version information and exit SIGNAL may be a signal name like `HUP', or a signal number like `1', or the exit status of a process terminated by a signal. PID is an integer; if negative it identifies a process group. NOTE: your shell may have its own version of kill, which usually supersedes the version described here. Please refer to your shell's docu- mentation for details about the options it supports. AUTHOR
Written by Paul Eggert. REPORTING BUGS
Report kill bugs to bug-coreutils@gnu.org GNU coreutils home page: <http://www.gnu.org/software/coreutils/> General help using GNU software: <http://www.gnu.org/gethelp/> COPYRIGHT
Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
kill(2) The full documentation for kill is maintained as a Texinfo manual. If the info and kill programs are properly installed at your site, the command info coreutils 'kill invocation' should give you access to the complete manual. GNU coreutils 7.1 July 2010 KILL(1)
All times are GMT -4. The time now is 05:07 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy