0

In my bash script, I do:

ssh me9@some_mad_server.com;
cd ~/apple;
echo "Before Exit"
exit
echo "After Exit"

I never see Before Exit or After Exit. I can understand why I may not see Before Exist as my script at that stage is in another console. But I am confused if the Exit mean my script ends and hence why After Exit never gets logged.

Any help appreciated.

2
  • 1
    Can you show some real code? Commented Sep 4, 2013 at 20:15
  • 1
    The answer would actually depend on how your script looks like and how you run. Commented Sep 4, 2013 at 20:20

2 Answers 2

3

To execute a series of commands on a remote host, you need to pass them to ssh on the command line, not execute them after the ssh call. Like this:

ssh me9@some_mad_server.com '
    cd ~/apple
    echo "Before Exit"
'
echo "After Exit"

This uses a multiline string to pass multiple commands. An exit is implicit when the end of the string is reached.

Importantly, the commands in the quoted string are executed on the remote host, while the final echo is executed on the local server. I've indented the remote commands for clarity.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use screen for this.

screen -d -m <command>

Use screen -r to attach to that screen again

screen -r <screen ID>

1 Comment

It does work with the -d -m flags on the screen command. I'll edit.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.