Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I use grep output in an expect script?

653 views
Skip to first unread message

Gorge Bueso

unread,
Sep 8, 2014, 11:18:03 AM9/8/14
to
Hi,

I am using expect to ssh to a remote host and run a program on the remote machine which has a variable runtime. I need to wait until it finishes so I can grab the output file of this program. I am trying to use the output of grep to know when the process finishes. I am trying to capture the PID and use it in a loop to determine when it's finished, like this:

set program "nameOfProgram"
set psVar "ps -ef | grep $program | grep -v grep | {awk '{ print $2 }'}"

expect "$PROMPT" {
send "pidVar=`$psVar`\r"
}

while {1} {

sleep $waitTime
expect {
"$pidVar*$PROMPT" {
send "$psVar\r"
}
"$PROMPT" {
sleep 1
break
}
}
}

My issue is that '$pidVar' is only visible in the remote shell, not in my expect script. If I replace the while loop with 'interact', I can echo $pidVar but I can't seem to use it in the 'expect' statement inside the while loop. I hope the issue is clear.

I seem to be missing something fundamental. Any suggestions??

Rich

unread,
Sep 8, 2014, 11:29:02 AM9/8/14
to
Gorge Bueso <gorge...@gmail.com> wrote:
> Hi,

> I am using expect to ssh to a remote host and run a program on the
> remote machine which has a variable runtime. I need to wait until it
> finishes so I can grab the output file of this program.
> ...
> I seem to be missing something fundamental. Any suggestions??

Yes, why are you launching this program into the background if you
really want to wait until it is done.

Just launch it synchronously, so that when it exits your script next
runs, then you can pickup its output file and you are done.

Otherwise, you'll need to be more detailed as to what you are really
trying to accomplish.

Gorge Bueso

unread,
Sep 8, 2014, 12:15:10 PM9/8/14
to
On Monday, September 8, 2014 11:29:02 AM UTC-4, Rich wrote:
Thanks for the response. The program is run in the foreground. It returns the prompt after hitting return, but it continues processing and writing to a file. The program is nmon, if you are familiar. The post-processing file continues to grow until the nmon process is finished as shown in the grep.

Gorge

unread,
Sep 8, 2014, 12:43:36 PM9/8/14
to
I should clarify - I am not running the program in the background on purpose (not appending &). But the program is apparently running in the background. It is an executable, so I don't have the source.

Rich

unread,
Sep 8, 2014, 7:09:01 PM9/8/14
to
Note, when using the awful google groups to followup, please fix the
quoting that google groups breaks before you submit your followup.

Alternately, register an Eternal September account and utilize a real
news reader: http://www.eternal-september.org/

No, not familiar with nmon, but googling for it reveals that it is open
source. So you do in fact have access to the source.

Does nmon eventually exit once whatever you've asked it to accumulate
has accumulated?

If so, you might make use of this feature of the skimpy documentation
to monitor for its exit, and only pickup the output file once it has
exited:

Use -p and nmon outputs the background process pid

What is it you are asking it to collect? You might just be able to
collect what you want by pulling the data directly out of the /proc
filesystem instead of going through nmon.

What happens if you use this option: "-c <number> of refreshes" to
set a specific number of refreshes? Does it automatically exit in that
case. If yes, then you might use -c # and -p to monitor it for
exiting, then collect the file.

Gorge A Bueso

unread,
Sep 9, 2014, 11:26:00 AM9/9/14
to
Hi Rich,

Sorry about that - I'm now using a real news reader

Thanks for the -p tip, although I was originally able to use grep and
awk to get the pid without the -p flag.

I am asking nmon to collect data regarding the performance of the host
machine. I am interested in doing trend analysis over the long term on
several host machines. For now, I am just trying to get my script working.

Here's the cmd I am using to develop my script:
nmon_x86_64_rhel6 -F myfile sample -s 2 -c 30

-s seconds per sample
-c runtime in seconds

I made an mistake by not reading the documentation carefully. My
mistake was that I was using -s as samples per second, not seconds per
sample. My fault. Using -s and -c correctly, I can calculate how long
to wait until the process finishes so that I don't need to monitor the
PID.

Many thanks for all the help and patience.

0 new messages