8

I left the office earlier with an active terminal window running a script on a Centos 7 server. Now I've SSHing in from home and I want to see where it's got to. Is this possible? Can I rejoin the same terminal window from here?

6 Answers6

15

As @Sven mentioned, the best option is to use screen or tmux. These are tools known as "terminal multiplexers". They allow you to create shell sessions which can be attached and unattached from actual logins. These tools aren't only useful to check your work from another terminal, but have other features, including sharing your session with another user and making sure that your command doesn't stop if you loose your internet connection. If you're searching for screen, you might try a search for "GNU Screen". Both of these tools are available on most Linux systems.

Typically, you would start the session, and then execute your command inside of that session. However, if you have already started the command, you might want to look up an article on moving a running command into a screen session. I wouldn't recommend trying this out the first time on something important, though. This question may be of some use:

Moving an already-running process to Screen

If you only want to check to see if the process is running, my favorite tool would be strace. This tool allows you to see each kernel call made by a process. It can take some skill to understand the output, but it should at least give you an idea if the process is running, and if you watch close enough, might catch the filenames it's opening. To do that, first, find the PID, maybe by searching through ps aux|grep yourcommand, and then:

strace -fp YOUR_PID

You can ^C to get out of that. It may not allow you to re-attach, but if you just want to know what it's doing, that should be sufficient.

DKing
  • 826
9

One way to do it: TMUX

As most answers already pointed out - if in an existing SSH session - you use tmux (or screen) with the command

tmux

You now are in a new bash session, in which you can start your program / command. You can close it anytime (but not with CTRL+D, rather by closing the window) and return to it later by building up an SSH connection to the same user at the same machine and writing the command

tmux attach

You can also have multiple tmux sessions by giving them names with

tmux new -s myname

You can see a list of all open tmux session for your user with

tmux ls

and attach to a named tmux session with

tmux a -t myname

Find a comprehensive tmux cheat sheet here.

For a running program

The answers so far seem not to be aware of the fact that you can move an already running process to another tmux / screen as this answer points out. The program that does the job is called reptyr and under Ubuntu / Debian you can install it with a simple

sudo apt-get install reptyr

After that, find out the process ID of your running program (for example with top or htop), start a tmux session and a simple

reptyr PID

will reattach the running process to your tmux bash session.

Manu J4
  • 191
5

In the future, you can use screen or tmux and can then reattach a running session.

Sven
  • 100,763
3

I have been using byobu and I find incredibly powerful. Byobu is a GPLv3 open source text-based window manager and terminal multiplexer.

So you can open a byobu session by simply typing in "byobu". And then you can create new tab with Ctrl + F2, and move between tabs with Ctrl + F3 for back, Ctrl + F4 for forward. You can close a tab with Ctrl + F6 or detach from a session with Shift + F6. You later re-attach to session by simply typing in "byobu" again.

Byobu supports vertical and horizontal splits, Full screen among other fancy features. It's available on Ubuntu/Debian, CentOS/Fedora/RedHat and FreeBSD.

2

As it was already pointed out, I recommend using tmux or screen in the future, but that only works if you thought about it beforehand.

I've been in your situation quite a few times. If the terminal you left running was in your office desktop PC, you can SSH into your PC and start a VNC server. That way, you can at least check the open terminal.

Pablo M
  • 298
0

As everyone mentioned, tmux can be used. screen is not useful where you want to share the screen with multiple people.

Above all I recommend everyone to tryout https://tmate.io/ to give temporary access, even to localmachine. tmate supports all the above :)

https://tmate.io/

Anto
  • 139