Languages and Scripting
Showing results for 
Search instead for 
Do you mean 

Killing a process witha script PART 2

SOLVED
Go to Solution
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
Honored Contributor Honored Contributor

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.
Honored Contributor 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!
Honored Contributor 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"
Super Advisor

Re: Killing a process witha script PART 2

Thank you all again!!!
Honored Contributor 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
Highlighted
Super Advisor

Re: Killing a process witha script PART 2

closed