Visit Our UNIX and Linux User Community


Killing specific process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Killing specific process
# 1  
Old 10-24-2007
Killing specific process

Hello,
I need to create a process that will kill a specific process if it's running. Let's just say the process is called win, actually called something else. It could be running multiple times on the machine and I would want to kill them all. Below is the code I have written so far, and it works but it picked up a processed called windat. I just want to kill the process if it's called win. Thanks.


Code:
for pid in `ps -elf | awk '{print $4"|"$15}' | grep win`
do
  PIDID=`echo $pid | cut -d"|" -f1`
  PROCESS=`echo $pid | cut -d"|" -f2`
  log "Killing $PIDID $PROCESS"
done

# 2  
Old 10-24-2007
Do you have "pkill" installed?
# 3  
Old 10-24-2007
We have a mix of Suse 9 and SCO boxes out in our environment, the Suse boxes have pkill but not the SCO. Thanks.
# 4  
Old 10-24-2007
Code:
#!/bin/sh

PROGRAM=$1

matches()
{
        if test "$1" = "$PROGRAM"
        then
                return 0
        fi

        return 1;
}

ps -e | while read PID TTY TIME CMD
do
        if matches $CMD
        then
                echo $PID
        fi
done

save this as a script, then run with program name as argument
# 5  
Old 10-25-2007
Thanks... That's a nice little script, I can see uses for that in some other things I am doing. Thanks!
# 6  
Old 10-25-2007
I've modified the script in a attempt to see if a process was killed or not, should be simple I thought. I set the flag to N at the beginning, and if in the if matches statement is true I set the flag to Y. After it's done with the while loop the match flag is N again. Why is that MATCH flag not global inside the while loop? something is eluding me here and I'm not sure what.

Code:
PROGRAM=$1
LOGFILE=/usr/local/logs/processkill.log
MATCH="N"
matches()
{
        if test "$1" = "$PROGRAM"
        then
                return 0
        fi

        return 1;
}

ps -e | while read PID TTY TIME CMD
do
        if matches $CMD
        then
          echo "Killing PID $PID for $CMD"
          MATCH="Y"
        fi
done
echo "$MATCH"

# 7  
Old 10-25-2007
Quote:
Originally Posted by benefactr
Why is that MATCH flag not global inside the while loop?
Because the while loop runs in a separate child process, this is due to the "|".

You could rearrange things and put the while read loop in a function and use it's exit code instead of a variable.

Previous Thread | Next Thread
Test Your Knowledge in Computers #281
Difficulty: Easy
A hypervisor or virtual machine monitor (VMM) is a computer software, firmware or hardware that creates and runs virtual private networks.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Killing the process ID's

Hi , I have a list of application process id's. Is there a way to kill all the process listed below using the script, except the once which are starting with " Genesis " adm 1522 ABC_Process.tra adm 1939 Genesis_Process.tra adm 2729 Genesis_Archive.tra adm 3259 xyz_Process.tra (5 Replies)
Discussion started by: murali1687
5 Replies

2. Homework & Coursework Questions

Unix Script -- To Skip killing a specific process

Hi, By using ps -aux | awk '/mine/{split($15,a,"/");print $1,$2,a}' i get the below listed PID's with there corresponding processes. adm 1522 ABC_Process.tra adm 1939 GENE_Process.tra adm 2729 GENE_Archive.tra adm 3259 xyz_Process.tra I use ps -aux | awk... (5 Replies)
Discussion started by: murali1687
5 Replies

3. UNIX for Advanced & Expert Users

Killing A Stopped Process

UNIX Tutorial Five % kill %jobnumber Does that not work on a stopped process? I've tried to kill a stopped process and it is not working. Or do you need a certain type of shell for this to work? I don't see anything about this in my man pages. (3 Replies)
Discussion started by: cokedude
3 Replies

4. Shell Programming and Scripting

Finding the age of a unix process, killing old processes, killing zombie processes

I had issues with processes locking up. This script checks for processes and kills them if they are older than a certain time. Its uses some functions you'll need to define or remove, like slog() which I use for logging, and is_running() which checks if this script is already running so you can... (0 Replies)
Discussion started by: sukerman
0 Replies

5. Shell Programming and Scripting

Help needed in killing a process

I'm trying to kill a process which is in sleep mode and the parent PID is 1 but I can't kill it with "kill -9" command. Is there a way to kill this process without rebooting? Any help will be appreciated. Steve (10 Replies)
Discussion started by: stevefox
10 Replies

6. Shell Programming and Scripting

Killing process!!!!

Hi friends, i m in big trouble.... i have one script which connects two server ...like below.. script1.sh ------------------------------------- bash test.sh & eval x=$@ export x=`echo $x` #echo $x # ssh user@8.2.5.6 bash /mbbv/location/script.sh $x|sed '/Binary file/d'... (1 Reply)
Discussion started by: Shahul
1 Replies

7. Shell Programming and Scripting

Killing process and children

Hi all, I have been searching all day for a nice solution to this problem. I have three scripts. A start script, a child script and a stop script. Script A (scripta.sh) Its Child Script B (scriptb.sh) Script C (kill_process.sh $PID) Script A correctly traps the kill command sent from... (6 Replies)
Discussion started by: mark007
6 Replies

8. UNIX for Dummies Questions & Answers

killing the process

Hi, First, I am running a scipt.While the script is running I realize that I dont want the script to be run so I am killing the script externally.Before the process gets terminated or killed it should delete all the temporary files created by the script.How to do this?Can anyone help me? ... (3 Replies)
Discussion started by: arthi
3 Replies

9. Shell Programming and Scripting

Killing of a process and send a mail if the process doesnot come up within 2 minutes

Hi Friends, I am new to this forum as well as new to shell scripting. I have a problem here and i need someone to solve this. Let us consider there are two processes(abc & def).There is a script which kills these two processes(i.e killtheprocess abc). Here abc is the argument . There is a... (1 Reply)
Discussion started by: Prince89
1 Replies

10. UNIX for Dummies Questions & Answers

killing a process

I can kill running processes on my linux red hat system using ctrl-c but cannot do it from command line of another terminal using kill -2 pid. Although I can kill them from command line using kill -9 pid and other signals. I would like to do it using the kill -2 pid. Thanks for your suggestions (6 Replies)
Discussion started by: bbhayana
6 Replies

Featured Tech Videos