I have python process running from a shell on a Linux. At the moment I don't have physical access to this machine and only can connect through ssh.
I know that the python process stopped and I suspect there is an error trace in this shell.
I can get pid of the shell but I can't find a way to display what is in this shell. Basically, I am looking to find a way to read what is already there and do not lose this output or close the shell. What would be the Linux tool to have a second view of what is going on in already running shell?

  • Has the shell been run through a terminal (ie did you launch it from a GUI)? – davidgo Jan 23 at 18:39
  • You could install a VNC server and then connect to it, to view the terminal shell? If you used a tty then I'm not sure how to view it remotely. – QuickishFM Jan 23 at 18:43
  • @davidgo the shell was started when on gnome and it is tty. I can't install VNC on this machine – tomasz74 Jan 23 at 18:46
  • I would recommend going forward to use: linux.die.net/man/1/screen – kylie.a Jan 24 at 1:42

There's this answer on ServerFault:

setterm can dump the contents of a Linux virtual console:

setterm -file log.txt -dump [ttynumbers]

However, it does not have access to the scrollback buffer, only to what's currently displayed.

This will work if you want to peek into /dev/tty?, not /dev/pts/*, I think. Use ps -e to identify the tty number of the shell in question. During my tests I have successfully dumped /dev/tty2 with the following command:

sudo setterm -file log.txt -dump 2

While you cannot view what's already happened (unless you can get that session back) there's an excellent tool called tmux which helps with this.

Simply install (sudo apt install tmux), run it (tmux) and then run the process you want inside the tmux terminal. Then hit Ctrl+B, then D, to detach it - this will get you back to your previous shell and you can close the terminal. Then, connect to it from SSH or open another terminal (whatever you fancy) and run tmux attach. That will get you back to your terminal. There are other options to explore, but this is a basic form of using tmux across different terminal sessions.

  • 3
    This should be a comment as it does not answer the OP's question. (Also, the screen utility does something similar) – davidgo Jan 23 at 20:44

The traditional tool for recording a terminal session is script. It is widely available, usually by default, on most Unix and Unix-like systems.

Some terminal emulators, including xterm of course, and some pc terminal emulators often used on the so-called "console" device of a desktop machine (i.e. the directly connected keyboard and display), do have a scrollback buffer and you can scroll back to see previous activity, and you might even be able to copy and paste it from the scrolled back view.

Indeed, the lesson here is that (most) TTY devices do not record anything -- they simply pass it through. One must arrange for one's own recording, be that by starting a script or similar recording tool, or by using an appropriate terminal emulator.

(BTW, some systems also have a "console buffer" as well, but it only records output from the kernel itself, and it is accessed with a command usually called dmesg.)

  • Can script work retrospectively? – Kamil Maciorowski Jan 24 at 6:34
  • it is a very good tip, but I can't find a way to view an already existing output. Anyway, probably next time will run it with some script option. – tomasz74 Jan 24 at 9:03

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.