Re: Killing a process witha script PART 2 (324 Views)
Reply
Super Advisor
TheJuiceman
Posts: 462
Registered: ‎02-20-2002
Message 1 of 7 (324 Views)
Accepted Solution

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.
Honored Contributor
Bill Hassell
Posts: 14,217
Registered: ‎05-29-2000
Message 2 of 7 (324 Views)

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
Muthukumar_5
Posts: 4,030
Registered: ‎06-09-2004
Message 3 of 7 (324 Views)

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
Arunvijai_4
Posts: 4,194
Registered: ‎04-07-2005
Message 4 of 7 (324 Views)

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
TheJuiceman
Posts: 462
Registered: ‎02-20-2002
Message 5 of 7 (324 Views)

Re: Killing a process witha script PART 2

Thank you all again!!!
Honored Contributor
Nguyen Anh Tien
Posts: 923
Registered: ‎05-20-2004
Message 6 of 7 (324 Views)

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
Super Advisor
TheJuiceman
Posts: 462
Registered: ‎02-20-2002
Message 7 of 7 (324 Views)

Re: Killing a process witha script PART 2

closed
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.