期待崩溃运行exec命令

时间:2014-03-14 07:00:27

标签: expect

我有一个执行exec的期望脚本,可能需要一些时间(约5分钟)。

我已经复制了下面的脚本以及运行脚本的输出。

如果脚本超时,我会想到"超时"被打印出去了吗?

任何指针都将受到赞赏!

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

输出:

...
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" ]..."

在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
}

从Expect输出中,似乎功能正常运行。

1 个答案:

答案 0 :(得分:1)

请参阅http://wiki.tcl.tk/exec

默认情况下,exec调用会在执行命令时返回错误状态:

  • 返回非零退出状态,或
  • 向stderr发送任何输出

这第二个条件可能令人厌烦。如果您不关心stderr,请使用exec -ignorestderr

你应该总是catch一个exec电话。参考维基页面中的更多细节,但至少:

set status [catch {exec command} output]
if {$status > 0} {
    # handle an error condition ...
} else {
    # success
}