2014-03-14 36 views
0

我有一个期望脚本执行exec,可能需要一些时间(大约5分钟)。期望崩溃正在运行的exec命令

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

如果脚本超时,我会认为“超时”打印出来了?

任何指针将不胜感激!

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 
} 

从期望的输出,却仿佛功能正在运行好。

回答

1

参见http://wiki.tcl.tk/exec

当exec'ed命令默认exec调用返回一个错误状态:

  • 返回一个非0状态,或
  • 发出任何输出到stderr

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

您应该始终使用catch执行调用。在引用的维客页面中有更多详细信息,但至少为:

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