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?
-
Possible duplicate: https://unix.stackexchange.com/questions/367370/read-the-existing-content-from-another-terminal/367379#367379 – Jakuje Jun 29 '17 at 07:11
-
I'd also like to add https://tmate.io to all the answers below – webknjaz Jun 29 '17 at 15:13
-
One option is: `strace -e write -p 12345` (substitute the pid of the process you are interested in). Another option is: `strace -e read -p 12345` (substitute the pid of the terminal emulator). In principle it would be possible to write a program to use the `ptrace` system call to collect the information in the same way that `strace` does, and write it directly to its own terminal. I don't know if such a program exists, but it would cover the requirements specified in your question. – kasperd Jul 01 '17 at 16:33
-
one possible way, enable RPC (remote procedure calls) on the target computer then you can (remotely) connect to and control the screen, including individual windows. Note: this opens a vulnerability so be sure to disable RPC when done. – user3629249 Jul 05 '17 at 23:02
6 Answers
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.
- 796
- 4
- 13
-
-
@HSchmale I'm not certain that I understand your question. If you are referring to strace, that program doesn't give you a terminal at all. It just shows you the kernel calls being made. Many times, when I suspect a process is hung, strace has shown me the exact files it's working on at the moment, so I know about where it's at, and that it's not hung. – DKing Jun 29 '17 at 13:19
-
It was a missread by me I thought you wouldn't be able to reattach strace to it, but you meant reattach a terminal to it. – HSchmale Jun 29 '17 at 16:00
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.
- 191
- 4
In the future, you can use screen
or tmux
and can then reattach a running session.
- 97,248
- 13
- 177
- 225
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.
- 294
- 5
- 13
-
Seems a little misleading to call byobu a terminal multiplexer. It's really just a front-end for `tmux` and `screen` that many find provides a better user-experience than they do out of the box. – B Layer Jul 14 '17 at 08:23
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.
- 298
- 1
- 6
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 :)
- 139
- 6