Start remote script via ssh containing nohup

I want to run the script remotely via ssh as follows:

ssh user@remote.org -t 'cd my/dir && ./myscript data my@email.com' 

The script does various things that work fine until it reaches the line with nohup:

 nohup time ./myprog $1 >my.log && mutt -a ${1%.*}/`basename $1` -a ${1%.*}/`basename ${1%.*}`.plt $2 < my.log 2>&1 & 

it is supposed to run the myprog program, transfer its output to mylog and send an email with some data files created by myprog as an attachment, and the log with the body. Although, when the script reaches this line, ssh produces:

Connection to the remote site is closed.

What is the problem?

Thanks for any help

+7
bash ssh nohup mutt
source share
2 answers

Your team starts the process pipeline in the background, so the calling script will exit immediately (or very soon). This will cause ssh to close the connection. This, in turn, will cause SIGHUP to be sent to any process connected to the terminal that was created for the -t option.

Your time ./myprog process is protected by nohup , so it should continue to work. But your mutt not, and that will probably be the problem here. I suggest you change your command line to:

 nohup sh -c "time ./myprog $1 >my.log && mutt -a ${1%.*}/`basename $1` -a ${1%.*}/`basename ${1%.*}`.plt $2 < my.log 2>&1 " & 

therefore, the entire conveyor will be protected. (If this is not fixed, you may need to do something with file descriptors - for example, mutt may have other problems with the lack of a terminal - or for quoting, you may need to configure it depending on the parameters - but try now ...)

+5
source share

This answer may be helpful. So, to achieve the desired effect, you need to follow these steps:

  • Redirect all I / O to the nohup'ed remote command
  • Notify the local SSH command of the exit, as soon as this happens, start the remote process (s).

Quote is the answer that I already spoke about , in turn, quoting wikipedia

Broken background jobs, for example, are useful when registering via SSH, since background jobs can cause the shell to hang when you exit the system due to the race condition [2]. This problem can also be overcome by redirecting all three I / O streams:

 nohup myprogram > foo.out 2> foo.err < /dev/null & 

UPDATE

I just managed with this template:

 ssh -f user@host 'sh -c "( (nohup command-to-nohup 2>&1 >output.file </dev/null) & )"' 
+5
source share

All Articles