Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

expect + grep question 1

Status
Not open for further replies.

arcnon

Programmer
Aug 12, 2003
242
US
what I want to do is find a pid.
Grep the number to a new variable ($PID)
then kill that process.

my broken attempt
Code:
set varReturned send -- "ps -A |grep rad\r"
set PID "$varReturned |grep ([0-9]*)"
send -- "sudo kill $PID\r"

it error says:
wrong # args: should be "set varName ?newValue?"
while executing
"set varReturned send -- "ps -A |grep rad\r""
(file "restart.exp" line 26)


Being this is my first expect script I am clueless on how to fix this
 
when you type "ps -A |grep rad" it returns 1 line
"6992 p2- S 0:08.64 radiusd"

what I need is "6992" or whatever the number is at the time
and set it to a variable so I can use it to send a kill statment to the server

if your wanting to know exactly what I am trying to do
I am ssh(ing) to a server
get the process id with ps
kill the process
restart the process
 
ps -ef| grep rad| awk '{print $2}'| xargs kill

?

laters
zaxxon
 
Sorry, last post was what might be to in case you are root.
What you want is more like:

PID=`ps -ef| grep radiusd| awk '{print $2}'`

The pid will be in PID. And better get a more precise pattern than "rad", because there might be so many other processes with a "rad" in their names/paths or usernames, so that you might kill accidentally the wrong one :)


laters
zaxxon
 
I don't have access to a *nix box right now but I'm pretty sure that doing a
ps -ef | grep rad

will return 2 lines, not one.
a. the line with the process that you're probably looking for AND
b. the process for the grep command used to find the "rad" string.

a nice fix to return just ONE line is

ps -ef | grep [r]ad
 
Look at the thread right above "Using ssh to run multiple commands". This might be what you are looking for...
 
lazyrunner50,
You should put a link to the thread. The order threads are listed changes depending on when they are replied to. That means "the thread right above" another one, probably won't be above it when someone goes to look for it.

Do you mean this thread? thread822-1085685

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top