Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expect script does not work under crontab

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
like image 279
Redouane Nour Avatar asked Sep 21 '11 03:09

Redouane Nour


People also ask

Why crontab scripts are not working?

One of the most frequent causes for the crontab job not being correctly executed is that a cronjob does not run under the user's shell environment. Another reason can be – not specifying the absolute path of the commands used in the script.

Do Cronjobs run automatically?

The cron reads the crontab (cron tables) for running predefined scripts. By using a specific syntax, you can configure a cron job to schedule scripts or other commands to run automatically.


1 Answers

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
like image 151
catac Avatar answered Oct 12 '22 03:10

catac