Operating System - Linux
1748154 Members
3622 Online
108758 Solutions
New Discussion юеВ

Re: Killing a process witha script PART 2

 
SOLVED
Go to solution
TheJuiceman
Super Advisor

Killing a process witha script PART 2

Hey guys,

I'm using the suggestion from my previous post...

PID=(ps -ef|grep <[P]rocess> | awk '{print $2}')

to get the PID of a process that is running so I can execute a kill on it. This seemed to work the first time I ran it. Now I am getting a set of 3 numbers in my return, such as "12345 67890 09877". How can I correct this? Thanks.
6 REPLIES 6
Bill Hassell
Honored Contributor
Solution

Re: Killing a process witha script PART 2

Try NEVER to use grep and ps together! ps has many, many options and one of them eliminates the need for grep completely when you are looking for a specific process. Use the -C option to find exactly the process you are looking for:

UNIX95=1 ps -C -o pid=

Now the above eliminates both grep and awk because ps can provide all you need. As far as having multiple PID's returned, with the above examp[le, it means that there are multiple copies of the program running. To see them in detail, just change -o pid to -f.


Bill Hassell, sysadmin
Muthukumar_5
Honored Contributor

Re: Killing a process witha script PART 2

If you are using ps -ef then try negate with grep -v grep when using grep with it.

Simply,

PID=$(ps -ef | gre[ <[P]rocess> | grep -v grep | awk '{ print $2; })

However, It is not always useful. Try to use UNIX95 format. That is good.

--
Muthu
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Killing a process witha script PART 2

Hello,

It is not good practice to club "ps" and "grep" together, UNIX 95 variable which bill said is the best method.

# UNIX95= ps -C"exact_process_name" -o pid

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
TheJuiceman
Super Advisor

Re: Killing a process witha script PART 2

Thank you all again!!!
Nguyen Anh Tien
Honored Contributor

Re: Killing a process witha script PART 2

above of all. I prefer Unix95 format
# UNIX95= ps -C"exact_process_name" -o pid
HP is simple
TheJuiceman
Super Advisor

Re: Killing a process witha script PART 2

closed