通过包含nohup的ssh启动远程脚本 [英] starting remote script via ssh containing nohup

查看:218
本文介绍了通过包含nohup的ssh启动远程脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to start a script remotely via ssh like this:

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

该脚本可以完成各种工作,直到与nohup连在一起为止:

The script does various things which work fine until it comes to a line with 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会输出:

it is supposed to do start the program myprog, pipe its output to mylog and send an email with some datafiles created by myprog as attachment and the log as body. Though when the script reaches this line, ssh outputs:

与remote.org的连接已关闭.

Connection to remote.org closed.

这是什么问题?

感谢您的帮助

推荐答案

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

Your command runs a pipeline of processes in the background, so the calling script will exit straight away (or very soon afterwards). This will cause ssh to close the connection. That in turn will cause a SIGHUP to be sent to any process attached to the terminal that the -t option caused to be created.

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

Your time ./myprog process is protected by a nohup, so it should carry on running. But your mutt isn't, and that is likely to be the issue here. I suggest you change your command line to:

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

因此整个管道都得到了保护. (如果这不能解决问题,则可能有必要对文件描述符进行某些操作-例如mutt可能会在终端不在时出现其他问题-或根据参数的不同可能需要对引号进行调整-但请尝试现在...)

so the entire pipeline gets protected. (If that doesn't fix it it may be necessary to do something with file descriptors - for instance mutt may have other issues with the terminal not being around - or the quoting may need tweaking depending on the parameters - but give that a try for now...)

这篇关于通过包含nohup的ssh启动远程脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆