1820130 Members
3073 Online
109619 Solutions
New Discussion юеВ

Killing process id

 
CWCW
New Member

Killing process id

First of all, i want to:
1. Connect to a remote machine through it's IP address and login to it.

my code:
spawn " telnet 10.10.x.x."
expect "login:"
send " Letme\r"
expect "assword:"
send " 123\r"
expect "#"



2. Then find out if XXX programme is running

send " [exec /bin/ps aux | grep XXX \r"
(XXX is the programme i want to get its process ID, could
be apache etc)

3. After the executing the above code, list of process id of XXX
programme will be listed.

What i want is to catch the first line of list and save it into the
variable, where i can then pick up the process id from it and then kill
it.

I have also tried to pipe the out of this comamnd send " [exec /bin/ps
aux | grep XXX \r" into the file, but the file can not be saved on the
remote machine.

Can any one show me a good and working code

Thanks in advance


11 REPLIES 11
Mark Grant
Honored Contributor

Re: Killing process id

How about

kill `remsh 10.10.x.x ps -ef | grep XXX | awk 'print $2;exit}'`

A bit dangerous though
Never preceed any demonstration with anything more predictive than "watch this"
Graham Cameron_1
Honored Contributor

Re: Killing process id

Have a look at this thread, and the ones linked fom it...
--
http://forums1.itrc.hp.com/service/forums/parseCurl.do?CURL=%2Fcm%2FQuestionAnswer%2F1%2C%2C0x80771cc6003bd6118fff0090279cd0f9%2C00.html&admit=716493758+1065690431101+28353475
--
From dim and distant experience I think remsh does fummy things to "expect".
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Dietmar Konermann
Honored Contributor

Re: Killing process id

Mark's advise is indeed a little bit dangerous... the killing is done locally. Oops. :)

I usuall kill like this:

# kill $(UNIX95=1 ps -o pid= -C XXX)

Remotely this would be:

# remsh 10.10.x.x "kill $(UNIX95=1 ps -o pid= -C XXX)"

Obviuously, you like using expect with telnet :-)... so this could be an option:

spawn telnet 10.10.x.x
expect "login:"
send " Letme\r"
expect "assword:"
send " 123\r"
expect "#"
send "kill $(UNIX95=1 ps -o pid= -C XXX)\r"
expect "#"

Best regards...
Dietmar.
"Logic is the beginning of wisdom; not the end." -- Spock (Star Trek VI: The Undiscovered Country)
Mark Grant
Honored Contributor

Re: Killing process id

You are absolutely right Dietmar! Doh! Perhaps that post should be removed

Ok, in a valiant attempt to re-deem myself, how about

$KILLPID=`remsh 10.10.x.x ps -ef | grep XXX | awk 'print $2;exit}`
remsh 10.10.x.x kill $KILLPID

Never preceed any demonstration with anything more predictive than "watch this"
Vasudevan MV
Frequent Advisor

Re: Killing process id

Try this

send "pids=`/bin/ps aux | grep XXX | awk \'\{ print \$1\}\'`\r"
send "kill -9 \$pids 2>/dev/null\r";

I have used this in my expect script to kill the remote process.

Regards
Vasu
CWCW
New Member

Re: Killing process id

Thanks Vasu

will please explain your code

send "pids=`/bin/ps aux | grep XXX | awk \'\{ print \$1\}\'`\r"
send "kill -9 \$pids 2>/dev/null\r";

how can i check whether the would-be -removed process id is right, in other words,
can i first display with puts " "
that means i need a variable to store the value of send "pids= ....
and then call puts " the id is $myvariable"

how can be saved the output of send nto varaiable ??

rgds

Thanks all of you guys
waiting reply
Vasudevan MV
Frequent Advisor

Re: Killing process id

Basically it greps for XXX process from the output of ps or ps -ef, then picks the 1st field to get the pid of that process on the remote machine and kills it forcibly using -9 option.

Yes, you can display the output of ps or ps -ef on the screen by not storing in a variable:

send "/bin/ps -ef | grep XXX | awk \'\{ print \$1\}\'\r"

In addition to that you can remove 2>/dev/null in the kill command to see the error if it is unable to terminate the process.

Regards,
Vasu
CWCW
New Member

Re: Killing process id

Hi Vasu

this code does not work .

the comamnd send, sends this line of code.

pids=`/bin/ps aux | grep ftp | awk '{ print $1}

so only the above line is printed, and
`/bin/ps aux | grep ftp | awk '{ print $1}
is not exucuted at all.

thanks
CWCW
New Member

Re: Killing process id

hi,experts

I wanted to connect to a remote machine and find out the process of ftp running on the remote machine, in order to kill it.
Than i have run this expect script
---------------------------------------
send {ps aux | grep ftp | awk '{print $2}'}; send "\r"
expect -re {([0-9]+).*}
set thepid $expect_out(1,string)
---------------------------------------
and i have got this result.

202
5482
13215

The middle number (5482) is the process id i am looking for.
My question is:

1. How can i get only this number and
display it on the screen.

2. How can i save this number on a variable

3. how can i kill this process using this number.


thanks.
Mark Grant
Honored Contributor

Re: Killing process id

Do you really have to use an expect script?

It would be much easier to use a shell script thus.

#!/usr/bin/sh

$KILLPID=`remsh 10.10.x.x ps -ef | grep ftp | grep -v grep | awk 'print $2;exit}`
remsh 10.10.x.x kill $KILLPID
Never preceed any demonstration with anything more predictive than "watch this"
CWCW
New Member

Re: Killing process id

Hi. Guys.

I want to use expect, not shell ( but thanks).


this is the code i am using
---------------------------------------------

send {ps aux | grep ftp | awk '{print $2}'}; send "\r"
expect -re {([0-9]+).*}
set thepid $expect_out(1,string)
---------------------------------------
and i have got this result.
202
5482
13215
The middle number (5482) is the process id i am looking for.
My question is:
1. How can i get only this number and
display it on the screen.
2. How can i save this number on a variable
3. how can i kill this process using this number.
thanks.