Expect script does not work under crontab

Redouane Nour picture Redouane Nour · Sep 21, 2011 · Viewed 24.8k times · Source

I have an expect script which I need to run every 3 mins on my management node to collect tx/rx values for each port attached to DCX Brocade SAN Switch using the command #portperfshow#

Each time I try to use crontab to execute the script every 3 mins, the script does not work!

My expect script starts with #!/usr/bin/expect -f and I am calling the script using the following syntax under cron:

3 * * * * /usr/bin/expect -f /root/portsperfDCX1/collect-all.exp sanswitchhostname 

However, when I execute the script (not under cron) it works as expected:

root# ./collect-all.exp sanswitchhostname

works just fine.

Please Please can someone help! Thanks.


The script collect-all.exp is:

#!/usr/bin/expect -f

#Time and Date
set day [timestamp -format %d%m%y]
set time [timestamp -format %H%M]

#logging
set LogDir1 "/FPerf/PortsLogs"
et timeout 5
set ipaddr [lrange $argv 0 0]
set passw "XXXXXXX"


if { $ipaddr == "" } {
        puts "Usage: <script.exp> <ip address>\n"
        exit 1
}


spawn ssh admin@$ipaddr
expect -re "password"
send "$passw\r"

expect -re "admin"

                log_file "$LogDir1/$day-portsperfshow-$time"
                send "portperfshow -tx -rx -t 10\r"
                expect timeout "\n"
                send \003
                log_file

                send -- "exit\r"
                close

Answer

catac picture catac · Sep 13, 2012

I had the same issue, except that my script was ending with

interact

Finally I got it working by replacing it with these two lines:

expect eof
exit