[关闭]
@ysongzybl 2015-05-01T21:25:24.000000Z 字数 963 阅读 1179

Bash execute ssh command on background

bash


Example 1:

I want to run a script test.sh on local host
and the test.sh access server "eos" via SSH, on the background.
The test.sh contains :

  1. #!/bin/bash
  2. ssh eos 'sh -c "(echo "hello" > yang.txt"'

Solution:
Use nohup command on local machine:

  1. nohup ./test.sh &

Result:
In the yang.txt file on server eos, there is one word "hello".


Example 2:

The test.sh takes one parameter :

  1. #!/bin/bash
  2. ssh eos " echo $1 >> yang.txt"

Solution:
Use nohup command on local machine:

  1. nohup ./test.sh "song" &

Result:
In the yang.txt file on server eos, there is one word "song".

Explanation [2]:

Variables in single-quotes are not evaluated. Use double quotes:

ssh pvt@192.168.1.133 "~/tools/run_pvt.pl $BUILD_NUMBER"

The shell will expand variables in double-quotes, but not in single-quotes.
This will change into your desired string before being passed to the ssh command.

Reference

[1] http://stackoverflow.com/questions/29142/getting-ssh-to-execute-a-command-in-the-background-on-target-machine

[2] http://stackoverflow.com/questions/3314660/passing-variables-in-remote-ssh-command

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注