Red HatThis forum is for the discussion of Red Hat Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Am new to the forum and new to shell scripting as well. I am trying to write a script where I will be able to find the process id for a particular process and then kill it throught the script itself.
I am able to get the process id from the below command I got online:
ps ax | grep $1| cut -d ' ' -f 1
However, sometimes I am getting two process id's. I want to take the process id's and kill them through the script.
I also tried the killall -9 <processname> command and it did not wokr.
[farooq@dummy181 U01]$ killall -9 <processname>
farooq: no process killed
Could anyone please advise on how I can accomplish this. Would greatly appreciate any help.
If you have pgrep (-n/-o) that might be better for you. I don't know why killall would report that unless the process wasn't actually running at the time.
And what is the process? What is your script? Why do you get multiples? Do you want to kill newest, oldest, all, some other variation?
-- Both of you remind me - you shouldn't use -9 unless you have to use -9. Most of the time, a process will respond to -15 or whatever and be able to clean up after itself.
Last edited by slakmagik; 09-29-2009 at 05:06 PM..
Reason: A patch in time saves kill -9
IF the process is waiting on another process, you probably cannot kill the first process. An example of this would be a optical drive that has "locked up", if you have a process (say dvd+rw-format) running on that drive you will not be able to kill the process. In some instances you MAY be able to kill the process that the first process is waiting on.
Here is small script to kill process for given process name excluding grep and script itself, and check new ID if service is self-restartable (starts immediately), like "vino" (the reason for script):
Code:
#!/bin/bash
# script name: resetgivenproc
# Ljubomir Ljubojevic
# mailto:drlove monkey plnet.rs
declare -a killarr
GetProcessNumber(){
line=`ps aux | grep -e $processname | grep -v "grep" | grep -v $0`
killarr=(`echo "$line"`)
}
processname=$1
GetProcessNumber
OldNumber=$((${killarr[1]}))
echo
echo "Old vino-server process number is "$OldNumber
echo
echo "Killing vino-server process number "$OldNumber
echo
kill ${killarr[1]}
GetProcessNumber
NewNumber=$((${killarr[1]}))
echo "New vino-server process number is "$NewNumber
echo
if [ $OldNumber -eq $NewNumber ]; then
echo "Vino process number was not killed";
else
echo "Vino process number was successfully killed"
fi
sleep 3
exit 0
Thanks for the script. It is a very big script for me and honestly dint understand anything in it. However, appreciate your help. Guess will need to get a little more knowledgeble in Linux before I canuse some script like the one you mentioned below.
Thanks a lot though!!!!
Quote:
Originally Posted by DrLove73
Here is small script to kill process for given process name excluding grep and script itself, and check new ID if service is self-restartable (starts immediately), like "vino" (the reason for script):
Code:
#!/bin/bash
# script name: resetgivenproc
# Ljubomir Ljubojevic
# mailto:drlove monkey plnet.rs
declare -a killarr
GetProcessNumber(){
line=`ps aux | grep -e $processname | grep -v "grep" | grep -v $0`
killarr=(`echo "$line"`)
}
processname=$1
GetProcessNumber
OldNumber=$((${killarr[1]}))
echo
echo "Old vino-server process number is "$OldNumber
echo
echo "Killing vino-server process number "$OldNumber
echo
kill ${killarr[1]}
GetProcessNumber
NewNumber=$((${killarr[1]}))
echo "New vino-server process number is "$NewNumber
echo
if [ $OldNumber -eq $NewNumber ]; then
echo "Vino process number was not killed";
else
echo "Vino process number was successfully killed"
fi
sleep 3
exit 0
How can I check if I have pgrep? The process is a Jboss process and I am trying to create a script which would deploy/patch a setup and then restart it too. However I do not want to run the shutdown command as sometimes it does not work if the process is hung and hence want to use the kill command.
I checked and the process was running at the time.
I think I am getting multiples as the command is not correct and not giving the process id.
Quote:
Originally Posted by slakmagik
Welcome to LQ.
If you have pgrep (-n/-o) that might be better for you. I don't know why killall would report that unless the process wasn't actually running at the time.
And what is the process? What is your script? Why do you get multiples? Do you want to kill newest, oldest, all, some other variation?
-- Both of you remind me - you shouldn't use -9 unless you have to use -9. Most of the time, a process will respond to -15 or whatever and be able to clean up after itself.
Script uses simple arrray (killarr) to accept output from grep command (it's separated by black spaces) and then we remember second item from array
Quote:
OldNumber=$((${killarr[1]}))
purely to compare and see if killing the process was succesiful. Then we kill first process (if there is more then one, this script will NOT kill them all, just the first one):
Quote:
kill ${killarr[1]}
. Then we repeat the collection of the process ID for comparison.
script line: "line=.." takes output from "ps aux" command and sends it (pipe's it) to first grep command that will remove all lines without given name in it. Output of that commmand is piped to secon grep command that will leave only lines without word grep (the command we are currently using), and that output is piped to third grep command that will leave only lines without the name of the script itself ( $0 ). Since ther is no more commands, what is left of initial listing is assigned to array. $1 is reserved variable of given processname you write after the script/function name (first of ... , second given is $2, etc.)
Simplified script to just kill given process ID would be:
To keep it simple, you might want just to run it inside a loop several times, or just call it several times with the same process name. There should be no damage that way, it will just try to kill non-existing process 0.
I hope this will clear up some of your questions. Answers to the rest of the questions find on http://tldp.org/LDP/abs/html/
It is working in my script too. However would appreciate if all you Linux Guru's would be kind enough to explain you suggestions so that I can get a better understanding.
Type 'which pgrep' (or 'type pgrep' if using bash) or just run the command.
Quote:
Originally Posted by farooqkadri
The process is a Jboss process and I am trying to create a script which would deploy/patch a setup and then restart it too. However I do not want to run the shutdown command as sometimes it does not work if the process is hung and hence want to use the kill command.
I checked and the process was running at the time.
I think I am getting multiples as the command is not correct and not giving the process id.
Well, many programs spawn multiple processes. It sounds like to me the 'does not work if the process is hung' is the real issue. If you're just trying to stop a system daemon, do something to it, and restart it, then that would be what the system scripts are for. So I'd look first at system scripts and application configuration. But I have zero knowledge of jboss, so good luck.
One thing to note, though, is that an app like that should dump a pid file somewhere (likely /var/run or some app-specific directory). Just kill the pid (or pids) in the file and you won't have to go hunting through the process table with a shotgun.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.