gpt4 book ai didi

bash - 通过包含 nohup 的 ssh 启动远程脚本

转载 作者:行者123 更新时间:2023-11-29 08:56:48 24 4
gpt4 key购买 nike

我想像这样通过 ssh 远程启动脚本:

ssh user@remote.org -t 'cd my/dir && ./myscript data my@email.com'

该脚本执行各种工作,直到遇到 nohup 行:

nohup time ./myprog $1 >my.log && mutt -a ${1%.*}/`basename $1` -a ${1%.*}/`basename ${1%.*}`.plt $2 < my.log 2>&1 &

它应该启动程序 myprog,将其输出通过管道传输到 mylog 并发送一封电子邮件,其中包含 myprog 创建的一些数据文件作为附件,日志作为正文。虽然当脚本到达这一行时,ssh 输出:

Connection to remote.org closed.

这里有什么问题?

感谢您的帮助

最佳答案

您的命令在后台运行进程管道,因此调用脚本将立即退出(或很快退出)。这将导致 ssh 关闭连接。这反过来又会导致 SIGHUP 被发送到连接到 -t 选项导致创建的终端的任何进程。

您的 time ./myprog 进程受 nohup 保护,因此它应该继续运行。但是你的 mutt 不是,这很可能是这里的问题。我建议您将命令行更改为:

nohup sh -c "time ./myprog $1 >my.log && mutt -a ${1%.*}/`basename $1` -a ${1%.*}/`basename ${1%.*}`.plt $2 < my.log 2>&1 " &

因此整个管道都得到保护。 (如果这不能解决问题,则可能需要对文件描述符做一些事情 - 例如 mutt 可能会遇到终端不存在的其他问题 - 或者引用可能需要根据参数进行调整 - 但请尝试现在……)

关于bash - 通过包含 nohup 的 ssh 启动远程脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4113168/

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