1

I don't know exactly what the correct term is, but I notice if I log in to a remote SSH server, then close the window, open a new one, and log into that server again, my bash history and user processes appear to be different. For instance, if I started a background process I can't get back into it, or something I typed won't show up in my bash history.

The problem is for some reason occasionally something happens to my remote session and instead of being disconnected the session just hangs; I have to close the window and open a new one to reconnect. As a result sometimes it means a long running process basically is "lost" since I can't get back into it.

Is there any way to set it up so that when I log back in I log back in to the same "session"?

This is using OS X Terminal.

3

2 Answers 2

4

When you log in to the remote machine, the sshd there allocates a pseudo-terminal and starts your login shell. Any processes you start, background or foreground, are child processes of that shell. (Read up on "fork", "parent process", and "child process"; use the "pstree" command to look at the state of the system.)

If you disconnect, for example by closing your client, the sshd will close the pseudo terminal. This will (indirectly, read up on "controlling terminal" and "session leader") terminate the shell process, which causes its children to either terminate or, because their parent is now gone, be reparented to init. (Read up on "HUP signal" - should be in whichever controlling terminal materials you find - and the nohup command.)

When you connect again, the sshd allocates a new pseudo-terminal and starts a new instance of your login shell. There is no mechanism to get the previously-running processes - assuming they are still running - attached to this shell (if there were, then every program ever would need to know how to deal with spontaneously appearing child processes that may want to share the standard in-/output and so on; a "shell" is a special process only to you, the user, not as far as the system is concerned).

The only way to do that is to use a program like screen or tmux (there may be others). These are essentially a server process that manages its own pseudo-terminals with shells and whatever else and a client process that can display those ptys' contents to you and send your input to them; when you disconnect, that client process dies, but when you reconnect, you can start a new one - the server will still be running.

(There are some over-simplifications in the above, but that should get you started to understand what's actually happening. Read up on "pseudo-terminal".)

0

Well actually, there is no way you can recover your session, what you can actually retrieve is your history.

To do so (in bash), you have to include (or alter) a line in your .bash_profile in your home directory to say bash to save your typed commands:

export HISTSIZE=1000 # to keep a thousand commands in history
export HISTCONTROL=ignoreboth # to prevent some strange behaviors

Then every command, until that thousand are reached, are write down to .history file, you can look at the last ones by typing

history

And search they by pressing Ctrl+R followed by a part of the command you wanna retrieve.

Let me say this history is in servers disk, so if you are in a shared server, other users commands should be in the same history file, and so, you should see theirs commands instead of your sometimes, the Ctrl+R should solve this problem.

PS: to apply this answer, be sure you are running inside bash, just type

ps

and look for an bash somewhere in the screen. Like this:

  PID TTY          TIME CMD
24165 pts/3    00:00:00 bash
24169 pts/3    00:00:00 ps

and you can learn a lot more in

man bash

But this would take a huge time to read.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .