I am running the following SSH command:
ssh "root@$ip" 'nohup sh -s' < ./do_sync &
My goal is to execute the contents of do_sync
(a shell script) on the remote host. I'm doing it this way because, for reasons I won't go into, I can't transfer the actual script file to that machine and execute it there.
Not sure if it's relevant, but the remote machine is actually an Android x86 device. However, I'm not doing anything Android-related on it. The Java VM isn't even running. It's just Android's version of Linux, with some busybox utilities on it. It has no bash
and the sh
it uses seems very basic/old. I'm not sure if it's fully POSIX compliant. It hasn't been until now that I've run into issues getting the behavior I want, so just in case it's OS-related I wanted to mention this detail.
This command works, but the ssh
process remains active in the background. What I expect to happen is this:
- Execute the SSH command in the background so that the script returns immediately.
- The SSH command should connect to the host, execute the
sh -s ...
commands and immediately return. - The SSH process from step 1 should end
Can someone help me get this behavior? Note that the machine I execute all of this on has bash, and newer version of sh. But the remote SSH host does not have bash, only a very old version of sh
. Not sure if that will matter, but thought I'd share anyway.
EDIT 1
I tried this as well but the behavior is the same:
cat ./do_sync | ssh "root@$ip" &
EDIT: Realized this is invalid, because I omitted the nohup
part, but this ended up having the same blocking behavior:
cat ./do_sync | ssh "root@$ip" "nohup sh -s"
EDIT 2
Tried this as well, still blocks the SSH connection from disconnecting:
commands="$(cat ./do_sync)"
ssh "root@$ip" "nohup sh -c $commands"
In this case for some reason sh -s
didn't work at all.
sh -s
process may not actually read the whole script to the end as soon as possibly, but only to the point it needs to to run it. This keeps the connection open. I'm assuming that when it reaches the end of input, the connection is terminated. Transferring the script to the remote host and running it there would alleviate this. – Kusalananda Dec 03 '19 at 19:13scp
on the remote host and it cannot be added. – void.pointer Dec 03 '19 at 19:54ssh remote 'cat >./do_sync && nohup sh ./do_sync &' <./do_sync
or something (untested) – Kusalananda Dec 03 '19 at 20:00cat ./do_sync | ssh "root@$ip" "cat > /do_sync && chmod 777 /do_sync && nohup /do_sync"
this still blocks, now I'm really confused.... – void.pointer Dec 03 '19 at 20:06&
in your last command? Not there. – Kusalananda Dec 03 '19 at 20:07sh
in BusyBox is of course the Almquist shell, as the BusyBox doco tells you. Confusing to people who erroneously think that the Bourne Again shell is definitive, but not actually the problem here. – JdeBP Dec 03 '19 at 20:47