2

I have a script which is composed of two parts. part1 creates an ssh connection, part2 runs a script (about 800 lines and counting so far).

part1:
ssh $target_server "bash -s" < $target_script

The problem is that part2 contains the following lines:

fc_START_WINE_APP () { nohup `DISPLAY=:11.0 wine "$1"` & }
. . .
fc_START_WINE_APP $applicationFull

Basically, it checks if an application is running, and if not restarts it (as well as some other stuff).

The problem is that, when the function is executed to start the application, it causes the ssh connection to hang.

Is there a way of forcing the ssh connection to exit when it gets to the end of the script.

I have tried exit, exit 0, exit 1 etc at the end of part2. I tried ssh -f in part1 but then part2 doesn't seem to execute.


UPDATE 2016-01-28

An example would be as follows

**SCRIPT1**
#!/bin/bash

target_server="1.2.3.4"
target_script="/home/user/script2.sh"

echo "Script 1 Started"

ssh $target_server "bash -s" < $target_script

echo "Script 1 Ended"

.

**SCRIPT 2**
#!/bin/bash

echo "Script 2 Started"

fc_START_WINE_APP () { nohup `DISPLAY=:11.0 wine "$1"` & }

applicationFull="/home/tech/myApp.exe"

fc_START_WINE_APP $applicationFull

echo "Script 2 Ended"

When running this It gets as far as Script 2 Ended, but doesn't close the ssh connection, and hence never gets as far Script 1 Ended.

As you can probably tell, the main point of the script is to connect to a given server, and start an (Old Windows) application using Wine.

My understanding is that it has to be started using nohup . . & to stop the application closing when the ssh connection is closed, in the same way that if I log in directly (using putty for instance), run nohup myCommand & and then exit, then myCommmand still runs.

  • 1
    Could you edit your question and add a minimal example that reproduces the error? – terdon Jan 25 '16 at 17:00
  • 4
    You have nohup `DISPLAY=:11.0 wine "$1"` & If those are actual backquotes, then what is the output of wine that the shell winds up running as a command passed to nohup? (If that command has a tty fd open, then sshd is going to wait until the command exits (or closes the tty fd).) If those are single quotes instead of backquotes, let us know. – Mark Plotnick Jan 25 '16 at 18:08
  • Could you please add a preamble to the question that describes what processes you would like to start at remote site, in what order, with which environment variables settings (e.g. DISPLAY) and what of started processes should stay active after ssh session termination. – Serge Jan 25 '16 at 19:39
  • please see above additions. – IGGt Jan 28 '16 at 14:25
1

When detaching program, you should redirect both outputs somewhere (to /dev/null or to log file), and use:

&>/dev/null &                         ...when bash --version >= 4

or

>/dev/null 2>&1 &                     ...all shells

instead of just:

&

... so that starting ssh do not need to worry about stdout, stderr and can exit.

nohup is not always important (depending of shell config and program's hup signal handling..., in my test: 'sleep', 'xclock' and 'wine notepad.exe' continue to work without need of nohup). And in situations when nohup is needed, usually screen and tmux are better solutions.

Assuming bash>=4, minimal example for testing detaching behaviour is that:

ssh 1.2.3.4 "sleep 10 &"               ... exits after 10 seconds
ssh 1.2.3.4 "sleep 10 &>/dev/null &"   ... exits immediately

In your specific script1/2situation, function fc_START_WINE_APP from script2 should be:

{ DISPLAY=:11.0 wine "$1" &>/dev/null & }

... works well for $1 value 'notepad.exe' ... but if you still want nohup for some reason, it accepts just one command, and you have 2 (setting display and runing wine), so you must wrap it inside bash:

{ nohup bash -c "DISPLAY=:11.0 wine $1" &>/dev/null & }
  • cheers, but the problem with using &>/dev/null & is that nothing gets written to the logfile from script2. I'll have to take a look at the screen and tmux options mentioned. – IGGt Jan 29 '16 at 10:06
  • @IGGt ... outputs from script2 will stay the same, only 'detaching command' must be redirected somewhere (to /dev/null , or to some log file). – Asain Kujovic Feb 1 '16 at 14:54
0

I think I have found the way to do this:

part1:
ssh $target_server "bash -s" < $target_script > logfile.log 2> logfile.err &

In this way the main script completes instantly, leaving the second script to run in the background.

  • 1
    No, it does not. This makes "part1" script to wait in the background. – RSFalcon7 Jan 28 '16 at 17:24
  • that's what I assumed, however, it does have the desired affect of connect via ssh, set the script running, and close the ssh connection. It's probably not ideal. – IGGt Jan 29 '16 at 10:08
0

Your ssh session needs a controlling terminal, for that use ssh -t

Also, you can execute part2 from ssh directly. So part1 will look like this:

**SCRIPT1**
#!/bin/bash

target_server="1.2.3.4"
target_script="/home/user/script2.sh"

echo "Script 1 Started"

scp $target_script $target_server:/tmp/my_script_part2.sh
ssh -t $target_server /tmp/my_script_part2.sh
ssh $target_server rm -f /tmp/my_script_part2.sh

echo "Script 1 Ended"
  • cheers, but when I tried that I still have the same problem. It executes as far as Script 2 Ended, and then doesn't go any further till I force the connection to close (ctrl-c), at which point it displays Script 1 Ended. – IGGt Jan 28 '16 at 16:35
  • The problem is that you needed a controlling terminal to properly handle all the SIGHUPS, and consequently not wait for the child processes. – RSFalcon7 Jan 28 '16 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.