获取ssh来在目标机器上的后台执行命令 [英] Getting ssh to execute a command in the background on target machine

查看:142
本文介绍了获取ssh来在目标机器上的后台执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个后续问题向怎么办你在一个shell脚本?问题使用ssh。如果我想,在这台机器上的后台运行远程机器上执行命令我如何才能ssh命令返回?当我尝试只包括符号(安培)在它只是挂起命令的结束。该命令的具体形式如下:

  SSH用户@目标CD /一些/目录;程序到执行和放大器;

任何想法?有一点要注意的是,登录到目标机器总是产生一个文本的旗帜,我的 SSH 键设置,因此无需密码。


解决方案

我在一个程序我写了一年前的这个问题 - 原来答案是相当复杂的。你需要使用的nohup以及输出重定向,如在 nohup的维基百科的解释artcle,这里复制您的方便。


  

Nohuping后台运行的作业是
  例如通过SSH登录时很有用,
  因为后台运行的作业可导致
  外壳挂在注销由于比赛
  条件[2]。这个问题也可以
  通过重定向所有三个被克服
  I / O流:

 的nohup myprogram> foo.out 2  - ; foo.err<的/ dev / null的&安培;


This is a follow-on question to the How do you use ssh in a shell script? question. If I want to execute a command on the remote machine that runs in the background on that machine, how do I get the ssh command to return? When I try to just include the ampersand (&) at the end of the command it just hangs. The exact form of the command looks like this:

ssh user@target "cd /some/directory; program-to-execute &"

Any ideas? One thing to note is that logins to the the target machine always produce a text banner and I have SSH keys set up so no password is required.

解决方案

I had this problem in a program I wrote a year ago -- turns out the answer is rather complicated. You'll need to use nohup as well as output redirection, as explained in the wikipedia artcle on nohup, copied here for your convenience.

Nohuping backgrounded jobs is for example useful when logged in via SSH, since backgrounded jobs can cause the shell to hang on logout due to a race condition [2]. This problem can also be overcome by redirecting all three I/O streams:

nohup myprogram > foo.out 2> foo.err < /dev/null &

这篇关于获取ssh来在目标机器上的后台执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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