LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices

Reply
 
Search this Thread
Old 09-29-2009, 03:45 PM   #1
farooqkadri
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Rep: Reputation: 0
Post Script to kill process id


Hi All,

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.
 
Old 09-29-2009, 04:02 PM   #2
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,907

Rep: Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326Reputation: 1326
Why not
Code:
kill -9 `pidof <processname>`
Cheers

Last edited by bathory; 09-29-2009 at 04:48 PM.
 
Old 09-29-2009, 04:02 PM   #3
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
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.

Last edited by slakmagik; 09-29-2009 at 04:06 PM. Reason: A patch in time saves kill -9
 
Old 09-30-2009, 08:07 AM   #4
lazlow
Senior Member
 
Registered: Jan 2006
Posts: 4,362

Rep: Reputation: 171Reputation: 171
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.
 
Old 09-30-2009, 09:26 AM   #5
DrLove73
Senior Member
 
Registered: Sep 2009
Location: Srbobran, Serbia
Distribution: CentOS 5.5 i386 & x86_64
Posts: 1,118
Blog Entries: 1

Rep: Reputation: 129Reputation: 129
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
Just type script name and process name:
Code:
resetgivenproc vino-server
 
Old 09-30-2009, 02:08 PM   #6
farooqkadri
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Hi Drlove73,

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 View Post
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
Just type script name and process name:
Code:
resetgivenproc vino-server
 
Old 09-30-2009, 03:03 PM   #7
farooqkadri
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Hi slakmagik,

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 View Post
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.
 
Old 09-30-2009, 03:06 PM   #8
farooqkadri
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Hi Bathory,

I tried the comamnd you suggested and below is the output.

First I did:

[sajith@dummy181 U01]$ ps -ef | grep modine
sajith 3731 3678 0 Sep30 ? 00:02:01 /U01/Nova/installed/jdk1.5.0_12/bin/java -Dprogram.name=run.sh -server -Xms512m -Xmx1024m -XX:MaxPermSize=128m -XX:+UseConcMarkSweepGC -XX:+CMSPermGenSweepingEnabled -XX:+CMSClassUnloadingEnabled -Dfile.encoding=UTF-8 -Duser.timezone=GMT -Dsystem.properties.load=true -DENVIRONMENT_SETTINGS_PROPS=/U01/Nova/Setups/Modine/EnvironmentSettings.properties -Djava.net.preferIPv4Stack=true -Djava.endorsed.dirs=/U01/Nova/JBoss_Blades/jboss-4.2.2G.A_50100_modine/lib/endorsed -classpath /U01/Nova/JBoss_Blades/jboss-4.2.2G.A_50100_modine/bin/run.jar:/u01/Nova/Setups_iAnalyze/Modine:/U01/Nova/installed/jdk1.5.0_12/lib/tools.jar org.jboss.Main -b 0.0.0.0
sajith 17030 10688 0 00:23 pts/11 00:00:00 grep modine
[sajith@dummy181 U01]$



===================================================================================
I tried the below command

[sajith@dummy181 U01]$ kill -9 `pidof modine`
-bash: pidof: command not found
kill: usage: kill [-s sigspec | -n signum | -sigspec] [pid | job]... or kill -l [sigspec]
[sajith@dummy181 U01]$

Could you please advise why it is not working?

Quote:
Originally Posted by bathory View Post
Why not
Code:
kill -9 `pidof <processname>`
Cheers
 
Old 09-30-2009, 03:06 PM   #9
DrLove73
Senior Member
 
Registered: Sep 2009
Location: Srbobran, Serbia
Distribution: CentOS 5.5 i386 & x86_64
Posts: 1,118
Blog Entries: 1

Rep: Reputation: 129Reputation: 129
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:
Code:
#!/bin/bash

# script name: resetgivenproc
# Ljubomir Ljubojevic
# mailto:drlove monkey plnet.rs

declare -a killarr

processname=$1
line=`ps aux | grep -e $processname | grep -v "grep" | grep -v $0`
killarr=(`echo "$line"`)

kill ${killarr[1]}
exit 0
you can even put this in separate function inside your script:

Code:
KillProcess () {
declare -a killarr

processname=$1
line=`ps aux | grep -e $processname | grep -v "grep" | grep -v $0`
killarr=(`echo "$line"`)

kill ${killarr[1]}
}
and call it with following syntax:

KillProcess <processname>

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/

Cheers!

Last edited by DrLove73; 09-30-2009 at 03:11 PM.
 
Old 09-30-2009, 03:08 PM   #10
farooqkadri
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 0
My command gives the below output:

[sajith@dummy181 U01]$ ps ax | grep modine | cut -d ' ' -f 1

17724
[sajith@dummy181 U01]$

However this is not the process id.


===================================================================================

I found a new command and my purpose is solved.

[sajith@dummy181 U01]$ ps -ef | grep modine | grep -v grep | awk '{print $2}'
3731
[sajith@dummy181 U01]$

I would still like to understand the suggestions that everyone has raised to gain a better understanding of Linux.

Thank you all for your help. Would appreciate if you all would be kind enough to explain your suggestions.
 
Old 09-30-2009, 03:12 PM   #11
farooqkadri
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Hi Dr love,

Thanks a lot. That explanation helped a lot and made my understanding a lot more clear. Appreciate all your help.
 
Old 09-30-2009, 03:15 PM   #12
farooqkadri
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 0
My command gives the below output:

[sajith@dummy181 U01]$ ps ax | grep modine | cut -d ' ' -f 1

17724
[sajith@dummy181 U01]$

However this is not the process id.


===================================================================================

I found a new command and my purpose is solved.

[sajith@dummy181 U01]$ ps -ef | grep modine | grep -v grep | awk '{print $2}'
3731
[sajith@dummy181 U01]$

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.

Thanks a lot!!!
 
Old 09-30-2009, 03:26 PM   #13
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Quote:
Originally Posted by farooqkadri View Post
Hi slakmagik,

How can I check if I have pgrep?
Type 'which pgrep' (or 'type pgrep' if using bash) or just run the command.

Quote:
Originally Posted by farooqkadri View Post
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.
 
Old 10-01-2009, 11:54 AM   #14
farooqkadri
LQ Newbie
 
Registered: Sep 2009
Posts: 12

Original Poster
Rep: Reputation: 0
Hi slakmagik,

I tried the command you suggested and below is the output:

[sajith@dummy181 ~]$ which pgrep
/usr/bin/pgrep
[sajith@dummy181 ~]$ type pgrep
pgrep is /usr/bin/pgrep
[sajith@dummy181 ~]$

So I guess I have pgrep. Thanks for the pointer!!! Appreciate all your help.
 
  


Reply

Tags
kill, process, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash `kill`: process 'B' silently dies; but process 'A' = `kill` spews back debris! GrapefruiTgirl Programming 9 06-23-2009 09:42 AM
Shell Script : Kill a running process when another process starts ashmew2 Linux - General 3 08-20-2008 03:47 AM
kill the process invoked from a shell script, when the script is killed kskkumar Linux - Software 8 05-23-2007 11:29 AM
kill process script adnanm Linux - Newbie 3 03-02-2007 12:30 PM
why my script die itself when I try to kill a process?? jaimese Linux - Newbie 4 10-27-2005 05:50 AM


All times are GMT -5. The time now is 06:49 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration