gpt4 book ai didi

linux - 在Linux中,如何使用ssh和expect在远程服务器上监控shell脚本执行的进度

转载 作者:太空宇宙 更新时间:2023-11-04 11:55:44 28 4
gpt4 key购买 nike

我在 shell 脚本 connect.sh 中有以下代码。远程脚本至少需要 1 小时才能完成执行。只有在远程脚本执行完成后,我才能在 $local_dir/file.tmp 中看到以下脚本的输出,我必须等待一小时。

如何在执行过程中并行监控远程脚本的输出/进度?

cat 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()

关于linux - 在Linux中,如何使用ssh和expect在远程服务器上监控shell脚本执行的进度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54415991/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com