SSH exits after quit case in bash script

5,362

Solution 1

The problem was in bash_aliases. I had to remove exec before the script. So now the bash_aliases file looks like:

command="/path/to/script.sh"

The exec causes SSH to exit session.

Solution 2

You could start a sub-shell from option 4, but when I try it with 'exec /bin/bash --login' the prompt of the sub-shell is lost, rendering the remote shell somewhat difficult to use.

The man page for ssh seems to suggest that you can run a shell Xor a command, but not both:

SYNOPSIS ssh [many options] [user@]hostname [command] ...

If command is specified, it is executed on the remote host instead of a login shell.

...

When the user's identity has been accepted by the server, the server either executes the given command, or logs into the machine and gives the user a normal shell on the remote machine.

...

The session terminates when the command or shell on the remote machine exits

Solution 3

If you run ssh remotehost somescript then the script runs and that's it. You only get an interactive shell if you don't specify a remote command: the interactive shell is the default remote command. When the script exits, the remote session is finished.

If you want to run an interactive shell after the script, then tell SSH that you want both. Add the -t option so that you get a terminal on the remote host: by default, SSH only creates a terminal when you tell it to run an interactive shell, not when you specify a command.

ssh -t remotehost '/path/to/script; exec bash'

Note that any environment variable defined in the script will not be available. If you need that, source the script in the remote shell instead. This also lets you close the session from the script: exit in the script will not just exit the script but the whole remote session since that's all running inside one shell.

ssh -t remotehost '. /path/to/script; exec bash'

Solution 4

Based on the behavior you're describing it sounds like you're running your script like so:

$ ssh remote picker.bash

Example

When I ssh into a remote system it works as expected, mainly that I stay the remote server once the script has completed.

Say I have 2 computers, remotey and lappy. We'll denote them using "L" for lappy, and "R" for remotey.

L$ ssh remotey

R$ ./picker.bash
1) option 1
2) Option 2
3) Option 3
4) Quit
Please enter your choice: 4

R$

However if I run the script like this, I get the behavior you're describing where the ssh terminal goes away:

L$ ssh remotey ./picker.bash
1) option 1
2) Option 2
3) Option 3
4) Quit
Please enter your choice: 4

L$
Share:
5,362

Related videos on Youtube

ispasov
Author by

ispasov

Updated on September 18, 2022

Comments

  • ispasov
    ispasov over 1 year

    So I have the following standard script:

        #!/bin/bash
        PS3='Please enter your choice: '
        options=("option 1" "Option 2" "Option 3" "Quit")
        select opt in "${options[@]}"
        do
            case $opt in
                "Option 1")
                    echo "you chose choice 1"
                    ;;
                "Option 2")
                    echo "you chose choice 2"
                    ;;
                "Option 3")
                    echo "you chose choice 3"
                    ;;
                "Quit")
                    break
                    ;;
                *) echo invalid option;;
            esac
        done
    

    But when I run the script through SSH and choose the Quit option, the SSH exits too. What might cause the problem? I only want to exit the script and to continue work in SSH.

    Well the problem was in .bash_aliases file which I discussed below. I was put exec before the script. When I remove it and run the command, everything was OK.

    • Rahul Patil
      Rahul Patil over 10 years
      are you running local script on remote server or what ?
    • Rahul Patil
      Rahul Patil over 10 years
      when you run script it's just like command ssh user@ip command it will execute and exit
    • ispasov
      ispasov over 10 years
      I am running a script situated on a remote Ubuntu server. First I log in with SSH and then I run the script - just enter the script path and name..
  • ispasov
    ispasov over 10 years
    Well if I run the command like this SSH doesn't exit. But I have to log into the server first, then run several operations before to run the script. I also have bash alias for the script. So could you tell me how to start the script in that way and to prevent SSH to exit.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 10 years
    @ispasov If you run SSH and get a shell prompt, then invoke the script from that shell prompt, then SSH does not exit.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 10 years
    @ispasov What exactly are you doing then? The problem is in some part that you don't describe in your question. Post the complete script and explain exactly how you invoke it.
  • ispasov
    ispasov over 10 years
    In my question I have posted the whole script as is. It's a test script so that's why it doesn't have any options. My steps are: 1. ssh user@server; 2. Password; 3. I am logged in the shell of the server and write the path and the name of the script /home/user/bin/script.sh; 4. When I choose option 4 - Quit, SSH exits.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 10 years
    @ispasov What you posted isn't a complete script since it doesn't have a shebang line. But even without a shebang line, the behavior you describe is extremely weird. What happens if you run a nested instance of bash and then start the script? What happens if you run bash -x /home/user/bin/script.sh? What happens if you run the script inside screen? What happens if you run the script locally?
  • slm
    slm over 10 years
    @ispasov - OK that's what I figured but the behavior sounded like the above. Can you share the rest of the script on the Ubuntu server. There is something else in the script besides the above which is causing it to exit.
  • ispasov
    ispasov over 10 years
    Here is the output: + PS3='Please enter your choice: ' + options=("Option 1" "Option 2" "Option 3" "Quit") + select opt in '"${options[@]}"' 1) Option 1 2) Option 2 3) Option 3 4) Quit Please enter your choice:
  • ispasov
    ispasov over 10 years
    And when I hit 4 option, the result is: Please enter your choice: 4 + case $opt in + break
  • ispasov
    ispasov over 10 years
    @sim This is the whole script. I just test it before add more commands to it and I get this weird behavior.
  • ispasov
    ispasov over 10 years
    Well I figured it out. The problem was in .bash_aliases file, not in the script. I accidentally put exec before the script
  • slm
    slm over 10 years
    @ispasov - Does the script not include the #!/bin/bash at the top then?
  • ispasov
    ispasov over 10 years
    Yes. But I found the solution (my mistake). Please see the end of my answer. Thank you for your help.
  • slm
    slm over 10 years
    @ispasov - Please write this up as an answer and accept it so that it's clear what the course of resolution was.