1

What happened:
I have entered ssh root@ip nohup java -jar app.jar & to run a jar on my remote server(the jar is an API server).
My local server is getting the jar logs instead of writing into nohup in the remote server and my jar is running but not working.

Expected result:
Able to SSH EXEC run nohup java -jar app.jar & from my local system.

Version:
Ubuntu 18.04

update:
jar's API URL is returning the output only when jar cmd is executed via putty.
For remote ssh exec, jar's API URL output is 404

update 1:
All I need is run the jar in the background in my remote server(exec the cmd from my local system).

2 Answers2

1

nohup and console outputs...

The point is to redirect output to the null device.

ssh user@host "sh -c 'cd /working/dir; nohup command -options > /dev/null 2>&1 &'"
# invoking sh makes output redirections easyier

ssh root@ip "sh -c 'nohup /path/to/java -jar /path/to/app.jar > /dev/null 2>&1 &'"

Of course, output can be saved to a file

nohup some_command > output.log 2>&1 &


EDIT after comments

to automate connection via ssh then start a program and interact (as display output logs), expect could do the trick.

Install expect on your local machine

sudo apt-get install expect

Create a small script, lets name it startItUp.sh and make it executable

#!/usr/bin/expect -f
spawn ssh user@host

# Its not recommended to insert a password in a script
# a good practice will be to copy key with ssh_copy_id
#expect "password: "
#send "myP@sswOrd\r"

# once ssh connection is enabled, lets expect for prompt
expect "$ "
# i got the prompt, i send a command
send "cd /go/to/needed/dir \r"

# again
expect "$ "
send "java -jar app.jar \r"

# let me interact (for example, i'll be able to ctrl+c)
interact
pa4080
  • 29,831
cmak.fr
  • 8,696
  • this stops me from getting logs back to my local server but my jar's API URL is returning 404 methods not found(the same jar works fine with normal cmd) @cmak.fr – AATHITH RAJENDRAN Nov 28 '19 at 06:51
  • @AATHITH RAJENDRAN, what means 'my local server'? If its related to output redirection, read the last line. About 404 and API URL, I dont understand the point, maybe because of the use of sh, try bash instead – cmak.fr Nov 28 '19 at 07:48
  • sry, local server = local system. How can I use bash remote instead of ssh remote?? – AATHITH RAJENDRAN Nov 28 '19 at 13:10
  • @AATHITH RAJENDRAN Oooh now its clearer : that was an XY... You do not want nohup but something like expect : You want to run a command (or a script) on your local machine which will start an ssh session and then run your Java prog. Ssh stays open and shows logs ?? If well understood, i will update my answser. – cmak.fr Nov 28 '19 at 17:17
  • @pa4080 : thank you ;) – cmak.fr Apr 23 '20 at 18:06
0

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
pa4080
  • 29,831
  • hey @pa4080 nohup java -jar app.jar server </dev/null >/dev/null 2>&1 & this cmd is working for me. how does this different from your answer?? – AATHITH RAJENDRAN Apr 23 '20 at 09:43
  • 1
    Hi, @AATHITHRAJENDRAN, I'm not familiar with your particular jar program -- but what you are doing is: you are feeding its stdin from </dev/null, and apparently you do not need a log file, because you redirecting the stdout to >/dev/null. Please read about /dev/null. – pa4080 Apr 23 '20 at 10:12
  • In addition you are redirecting the stdout >/dev/null on the remote instance, while in my answer it is redirected to a log file on the local instance... Is is not clear you are executing this command over ssh or directly within a shell on the remote machine... – pa4080 Apr 23 '20 at 10:30