Question

I have an expect script that performs an exec that can take some time (around 5 mins).

I have copied the script below and also the output from running the script.

If the script was timing out, I would have thought "timeout" was printed to std out?

Any pointers will be appreciated!

expect <<EOF
   cd /home/vagrant/cloudstack
   # 20 mins timeout for jetty to start and devcloud to be provisioned
   set timeout 1200
   match_max 1000000

   set success_string "*Started Jetty Server*"

   spawn "/home/vagrant/cloudstack_dev.sh" "-r"
   expect {
     -re "(\[^\r]*\)\r\n" 
     {
       set current_line \$expect_out(buffer)

       if { [ string match "\$success_string" "\$current_line" ] } {
          flush stdout
          puts "Started provisioning cloudstack."

          # expect crashes executing the following line:
          set exec_out [exec /home/vagrant/cloudstack_dev.sh -p]

          puts "Finished provisioning cloudstack. Stopping Jetty."
          # CTRL-C
          send \003
          expect eof
       } else { 
          exp_continue 
       }
     }
     eof { puts "eof"; exit 1; }
     timeout { puts "timeout"; exit 1; }
   }
EOF

The output:

...
2014-03-14 06:44:08 (1.86 MB/s) - `/home/vagrant/devcloud.cfg' saved [3765/3765]

+ python /home/vagrant/cloudstack/tools/marvin/marvin/deployDataCenter.py -i /home/vagrant/devcloud.cfg
+ popd
+ exit 0
    while executing
"exec /home/vagrant/cloudstack_dev.sh -p"
    invoked from within
"expect {
     -re "(\[^\r]*\)\r\n" 
     {
       set current_line $expect_out(buffer)

       if { [ string match "$success_string" "$current_line" ]..."

The function that gets run inside the cloudstack-dev.sh:

function provision_cloudstack () {

   echo -e "\e[32mProvisioning Cloudstack.\e[39m"

   pushd $PWD
   if [ ! -e $progdir/devcloud.cfg ]
   then
      wget -P $progdir https://github.com/imduffy15/devcloud/raw/v0.2/devcloud.cfg
   fi
   python /home/vagrant/cloudstack/tools/marvin/marvin/deployDataCenter.py -i $progdir/devcloud.cfg
   popd
}

From the Expect output, it seems as though the function is being run ok.

Was it helpful?

Solution

See http://wiki.tcl.tk/exec

the exec call by default returns an error status when the exec'ed command:

  • returns a non-zero exit status, or
  • emits any output to stderr

This second condition can be irksome. If you don't care about stderr, then use exec -ignorestderr

You should always catch an exec call. More details in the referenced wiki page, but at a minimum:

set status [catch {exec command} output]
if {$status > 0} {
    # handle an error condition ...
} else {
    # success
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top