![](/img/trans.png)
[英]How to run a bash or shell script in a remote linux server using perl script and ssh
[英]In Linux , how to monitor the progress of shell script during execution in remote server using ssh and expect
我在shell腳本connect.sh
有以下代碼。 遠程腳本將至少花費1個小時來完成執行。 僅在完成遠程腳本執行后,我必須等待一小時,才能在$local_dir/file.tmp
看到以下腳本的輸出。
如何在執行期間監視遠程腳本並行的輸出/進度?
貓connect.sh
:
#!/bin/bash
local_dir="/scratch"
/usr/bin/expect > "$local_dir/file.tmp" << EOF
set timeout -1
spawn ssh -o "StrictHostKeyChecking no" "user@host" "cd /u01; ./remote_script.py arg1 arg2 arg3 arg4"
expect "user@host's password:"
send "$pwd\r"
expect "*#*"
EOF
該日志將收集remote_script.py
打印的所有內容。 會有緩沖,因此,如果僅打印幾行,則在作業完成或編寫器強行刷新緩沖區之前,您將看不到它們。
作為一種解決方法,可以更改remote_script.py
以便將進度報告打印為標准錯誤。 Python logging
模塊使這一過程變得直觀而直觀。
import logging
def main():
logging.info('Starting up ...')
# Do startup stuff
for n in range(100000):
if n % 10000 == 0:
logging.info('Progress: {0}/100000'.format(n))
# do more things in each iteration
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO,
format='%(module)s:%(asctime)s:%(message)s')
main()
Expect必須緩沖其輸出以匹配模式。
您可以定義緩沖區大小,並在緩沖區填滿時采取措施。 像這樣:
set match_max 4096 ;# or some other value, not too small
expect {
full_buffer {
puts -nonewline "."
fflush stdout
exp_continue
}
"#" {puts ""}
}
send "exit\r"
expect eof
這
將
應該打印的數據看到每個4K點,是一種進步吧。 但是您尚未共享遠程腳本生成的數據量,因此,這實際上是一個暗影。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.