shell script to find a process by name and kill it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to find a process by name and kill it
# 1  
Old 07-26-2012
shell script to find a process by name and kill it

hi,

Am a newbie to unix and wasnt able to write script to my requirement.

I need a shell script, which should find a process by name and kill it. For eg: let the process name be "abc". I have different processes running by this name(abc), so should kill them all.

Condition would be: if this process(abc) is running, display a comment "process is running" and also kill all the processes associated with this name.
else
display a comment "process is not running" and then move to next step.

Thanks in Advance.
# 2  
Old 07-26-2012
try this!!

Code:
#!/bin/bash
pgrep $1 2>&1 > /dev/null
if [ $? -eq 0 ]
then
{
echo "process running"
ps -ef | grep $1 | grep -v grep | awk '{print $2}'| xargs kill -9
}
else
{
echo "no process running"
};fi

run:
Code:
./killscript.sh your_process_name

# 3  
Old 07-26-2012
What OS and shell are you using? ex: Solaris has pkill which does exactly what you want
# 4  
Old 07-26-2012
more detail please

hey Chidori,

thanks for that script, as I told you am newbie to unix, can you put that "abc" process in your example and edit the code. As I dnt find anywhere to put my process name in that code.

---------- Post updated at 02:24 PM ---------- Previous update was at 02:23 PM ----------

am using Solaris

SunOS 5.10 Generic_142900-14 sun4u sparc SUNW,Sun-Fire-V245
# 5  
Old 07-26-2012
The $1 and $2 are commandline parameters, you put them after the program when you run it.
# 6  
Old 07-26-2012
you can use the script to kill any process... just run like this
Code:
./killscript abc

# 7  
Old 07-26-2012
I already have a server script in place, I want to add this to existing script so that "abc" process gets killed when the main scripts executes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

2. Linux

Find a process ID,kill it and restart agent

#!/bin/bash #This shell finds the pid of the hawkagent and kills and restarts to put the rulebase into effect output=`ps aux|grep hawkagent` #The set -- below helps to parse the above ps output into words and $2 gives the 2nd word which is pid set -- $output pid=$2 #Checks if pid of hawkagent... (12 Replies)
Discussion started by: samrat dutta
12 Replies

3. Shell Programming and Scripting

Find the Pid and Kill the Process after a Few Minutes

hi guys i had written a shell script Display Information of all the File Systems i want to find the pid and kill the process after few minutes.how can i obtain the pid and kill it??? sample.sh df -a >> /tmp/size.log and my cron to execute every minute every hour every day * *... (5 Replies)
Discussion started by: azherkn3
5 Replies

4. Shell Programming and Scripting

Cron job and shell script to kill a process if memory gets to high

Hello, I'd like to set a cron job that runs a shell script every 30 minutes or so to restart a java based service if the memory gets above 80%. Any advice on how to do this? Thanks in advance! - Ryan (19 Replies)
Discussion started by: prometheon123
19 Replies

5. Shell Programming and Scripting

shell script to kill process with respect to terminal

Hi, I've a script which kills all process, but i need a script shell script(sh), where it'll kill process on that particular terminal. below is example TY=`tty` for P in $TY do `kill -9 $P 2>/dev/null`; done echo "test process killed" break ... (3 Replies)
Discussion started by: asak
3 Replies

6. Shell Programming and Scripting

Shell Script, Copy process using find.

Ok, so here is what I am looking for.. Shell script that uses find to look for one days worth of data using the modified date and then copies only those files to a specified directory. I figured I could use grep and the find command to do this. It seems to work just fine from what I can... (4 Replies)
Discussion started by: techjunky
4 Replies

7. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

8. Shell Programming and Scripting

Kill a process from parent shell within a shell script

Hi, I am looking for a solution for the following problem: Im Using tcpdump within a shellskript started in a subshell by using brackets: ( /usr/sbin/tcpdump -i ... -c 1 ) - I want the outout of tcpdump saved in a variable - Than tcpdump-Process in the Subshell should be killed - and I... (6 Replies)
Discussion started by: 2retti
6 Replies

9. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

10. Shell Programming and Scripting

Unix Script to find and kill a process with high memory utilization

Hi Unix Gurus i am somewhat new to unix scripting so need your help to create a script as below. # This script would find the process consuming memory beyond a certain #limit. if the meemory consumption is more than 100% for a period of 1 # minute for the specific process. the script would... (0 Replies)
Discussion started by: robinforlinux
0 Replies
Login or Register to Ask a Question