2014-03-14 73 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 
}