Interesting topic :)
First I thought, you must use single quotes in order to quote the remote command. Nice explanation, about this point, is provided by @pt314 within the accepted answer of the question How can SSH work with an if condition? Here is the nonessential part:
The main problem is that double-quoted strings are expanded by your
local shell...
But probably this is only a part of the problem. So before proceeding, let's say few words about the most canonical way of nohup
's usage that we can find on the Internet:
nohup comand -options arguments >/path/to/log 2>&1 &
| | | | # run 'nohup' in the background
| | | # redirect stderror to stdout
| | # redirect stdout of 'nohup', /dev/null when don't need it
| # the command to be executed, its stdout/err will be appended to the nohop's stdout
# run the following command detached from the current shell
According to the above, it looks you need a command like this:
ssh user@host 'nohup remote_command -optins arguments &' >/tmp/local.log
But, what actually happens here?
The stdout (standard output) of nohup
is attached to the ssh session and the output of the ssh
command is redirected to a file, within the current shell session. In other words, the remote command is attached yo the local shell session. So if we kill something on the chain (the ssh session or the local shell) everything fails. Apparently, in this case, we do not need nohup
on the remote session at all.
If we go one step further we can conclude that using nohup
on the local side is a good idea:
nohup ssh user@host 'remote_command -optins arguments' >/tmp/local.log 2>&1 &
This looks much robust, but the execution of the remote command still depends on the ssh session initiated from the local computer, so if, for example, it is rebooted everything will fail again.
I think there is plenty of possible ways to achieve that you want in a more safer way. Here is one suggestion.
1. Let's use nohup
in the canonical way to execute the remote command:
ssh user@host 'nohup remote_command -optins arguments >/tmp/remote.log 2>&1 &'
2. Fetch the remote log over ssh and write a local log in the background:
nohup ssh user@host 'tail -n+1 -F /tmp/remote.log' >/tmp/local.log 2>&1 &
3. Monitor the local log file by:
tail -F /tmp/local.log
ssh root@ip "nohup java -jar app.jar &"
? – George Udosen Nov 28 '19 at 05:52