How can I watch a terminal window remotely?

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?


Solution 1:

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.

Solution 2:

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.