Reputation: 21
Here are some test results:
I run command on my localhost, and try to execute some command on the remote host 11.160.48.88
ssh 11.160.48.88 "wget https://raw.githubusercontent.com/mirror/wget/master/README -O wgetReadme"
expect:
File can be downloaded and be renamed to wgetReadme
result:
work as expected
ssh 11.160.48.88 "wget https://raw.githubusercontent.com/mirror/wget/master/README -O wgetReadme&"
I simply add the &
at the end of command, because I want this command to run in background
result:
the file wgetReadme
is null on the remote server, I don't know why
To test if the Command 2
can be run on the remote server, I try to run the command directly on the server 11.160.48.88
wget https://raw.githubusercontent.com/mirror/wget/master/README -O wgetReadme&"
result: There are some wget transport message print to stdout, and the file is downloaded to wgetReadme. Work corretly.
I want to figure out if it is the SIGHUP
signal kill the subprocess, and I found two evidences to prove it is not.
11.160.48.88
$shopt|grep hup
huponexit off
So the subprocess will not receive SIGHUP
when ssh exits
ssh 11.160.48.88 "wget https://raw.githubusercontent.com/mirror/wget/master/README -O - 2>&1 > wgetReadme&"
result: The file can be downloaded to the target file correctly.
My question is why Command 2
cannot work as expected?
Upvotes: 1
Views: 417
Reputation: 1325
Because backgrounded jobs in ssh can cause the shell to hang on logout due to a race condition that occurs when two or more threads can access shared data and they try to change it at the same time and you can also solve the problem by redirecting all three I/O streams such as > /dev/null 2>&1 & So Nohup command is useful in your case and it is a POSIX command to ignore the HUP (hangup) signal. The HUP signal is, by convention, the way a terminal warns dependent processes of logout. So I change your code as following way:
ssh -f 11.160.48.88 "sh -c 'nohup wget https://raw.githubusercontent.com/mirror/wget/master/README -O - > wgetReadme 2>&1 &'"
You can read more at https://en.wikipedia.org/wiki/Nohup
Upvotes: 3
Reputation: 161
& is a bash special characters which make process running in background. Then , ssh will not capture anymore output of command when you run this remotely.
You should escape it with \ to be able to run your command
in your example :
wget https://raw.githubusercontent.com/mirror/wget/master/README -O wgetReadme\&"
regards
Upvotes: 0