Linux connect to process output

Attaching a process to a terminal in LInux

I have a file opened in vi running in a terminal( xterm ), if I directly closes the terminal without first closing the file then I can see the vi still running in the background( ps x ). Now is there any way to attach that process i.e. vi to some other terminal so that I can continue my work on the file. I have also tried fd command but it fails.

4 Answers 4

This is not possible “cleanly”. There are tools such as screen and tmux that create a remanent virtual terminal: you can start your program inside screen, detach the screen session, and later reattach the screen session on a different terminal. But that requires planning ahead.

It is possible to use ptrace to fiddle with the process’s file descriptors to reconnect them to another terminal. That doesn’t work reliably because it might create inconsistencies in the process’s data structures: some programs won’t mind, others will crash. You can do this by attaching the program in a debugger and issue the right sequence of system calls (at least open , dup and close for each of standard input, standard output and standard error). There are several existing tools that can do this, for example neercs and retty.

You can try GNU screen. It is made for this purpose.

Screen is a full-screen window manager that multiplexes a physical terminal between several processes, typically interactive shells

Источник

Can I use GDB to debug a running process?

Under linux, can I use GDB to debug a process that is currently running?

8 Answers 8

You can attach to a running process with gdb -p PID .

Yes. Use the attach command. Check out this link for more information. Typing help attach at a GDB console gives the following:

Attach to a process or file outside of GDB. This command attaches to another target, of the same type as your last » target » command (» info files » will show your target stack). The command may take as argument a process id, a process name (with an optional process-id as a suffix), or a device file. For a process id, you must have permission to send the process a signal, and it must have the same effective uid as the debugger. When using » attach » to an existing process, the debugger finds the program running in the process, looking first in the current working directory, or (if not found there) using the source file search path (see the » directory » command). You can also use the » file » command to specify the program, and to load its symbol table.

NOTE: You may have difficulty attaching to a process due to improved security in the Linux kernel — for example attaching to the child of one shell from another.

Читайте также:  Закодируйте с помощью кодировочной таблицы ascii windows

You’ll likely need to set /proc/sys/kernel/yama/ptrace_scope depending on your requirements. Many systems now default to 1 or higher.

Источник

How can I hook on to one terminal’s output from another terminal?

I need to hook onto output of currently running terminal (tty1) from virtual terminal and capture it (running X server).

8 Answers 8

I came across this one tool called ttylog . It’s a Perl program available on CPAN here. It has a couple caveats, one being that I could only figure out how to attach to a terminal that was created as part of someone ssh’ing into my box. The other being that you have to run it with elevated privileges (i.e. root or sudo).

For example

First ssh into your box in TERM#1:

Note this new terminal’s tty:

Now in another terminal (TERM#2) run this command:

Now go back to TERM#1 and type stuff, it’ll show up in TERM#2.

All the commands I tried, (top, ls, etc.) worked without incident using ttylog .

Indeed it is. The /dev/vcs* and /dev/vcsa* devices corresponds to the /dev/tty* devices (the virtual terminals). F1=tty1=vcs1/vcsa1 and so on. The vcs/vcsa is like tty for the «current» virtual terminal.

As root, you can just cat these devices (e.g. cat /dev/vcs2), and see what’s on the corresponding VT (e.g. /dev/tty2 the on on F2) like taking a snapshot. vcsa* differs from vcs* in that they include information about the dimensions of the terminal (the screen). Mind you, it’s just a raw snapshot of the characters as they show on the screen — gathered from the memory allocated to the terminal — so don’t expect nice, easily parseble output.

The drawback is that if the information flashes past too fast, it may be difficult to capture. Perhaps tail -f /dev/vcs1 will work, if you need to follow several screenfulls (haven’t tried myself)? It may be easiest to simply redirect it to a file first. It may also be a good idea to use a VT (F1-F6) to look at it, as the terminals will have the same dimensions. In my experience, it’s best to use the vcs* — not the vcsa* — devices.

If that doesn’t work, perhaps one of the «big brotherish» packages that allows an admin to keep an eye on the activity on a terminal may work.

PS: I forgot to ask what OS you use. This is for Linux, though similar devices probably exists on other OSes too. Try searching for «virtual console memory» among the man-pages for devices.

Читайте также:  Почему windows 10 не видит сетевое окружение

Источник

How to view the output of a running process in another bash session?

I have left a script running on a remote machine from when I was locally working at it. I can connect over SSH to the machine as the same user and see the script running in ps .

It is simply outputting to stdout on a local session (I ran ./ipchecker.sh form a local terminal window, no redirection, no use of screen etc).

Is there anyway from an SSH session I can view the output of this running command (without stopping it)?

So far the best I have found is to use strace -p 18386 but I get hordes of text flying up the screen, its far too detailed. I can stop strace and then sift through the output and find the text bring printed to stdout but its very long and confusing, and obviously whilst it’s stopped I might miss something. I would like to find a way to see the script output live as if I was working locally.

Can anyone improve on this? The obvious answer is to restart the script with redirection or in a screen session etc, this isn’t a mission critical script so I could do that. Rather though, I see this as a fun learning exercise.

11 Answers 11

You can access the output via the proc filesystem.

1 = stdout, 2 = stderr

If all you want to do is spy on the existing process, you can use strace -p1234 -s9999 -e write where 1234 is the process ID. ( -s9999 avoids having strings truncated to 32 characters, and write the system call that produces output.) If you want to view only data written on a particular file descriptor, you can use something like strace -p1234 -e trace= -e write=3 to see only data written to file descriptor 3 ( -e trace= prevents the system calls from being loged). That won’t give you output that’s already been produced.

If the output is scrolling by too fast, you can pipe it into a pager such as less , or send it to a file with strace -o trace.log … .

With many programs, you can divert subsequent output with a ptrace hack, either to your current terminal or to a new screen session. See How can I disown a running process and associate it to a new screen shell? and other linked threads.

Источник

Attach to a processes output for viewing

How would I ‘attach’ a console/terminal-view to an applications output so I can see what it may be saying?

How would I detach from an applications output without killing the application?

Normally if you fire up a talkative application using the command line you get to see all kinds of wonderful output. However, let’s say I have a particularly chatty programming running, like KINO, and I want to view its output at any given moment without restarting it through the command line. I cannot; at least I don’t know how.

Читайте также:  Драйвера для старых видеокарт ati radeon для windows 10 64 bit

7 Answers 7

I think I have a simpler solution here. Just look for a directory whose name corresponds to the PID you are looking for, under the pseudo-filesystem accessible under the /proc path. So if you have a program running, whose ID is 1199, cd into it:

Then look for the fd directory underneath

This fd directory hold the file-descriptors objects that your program is using (0: stdin, 1: stdout, 2: stderr) and just tail -f the one you need — in this case, stdout):

I was looking for this exact same thing and found that you can do:

It’s not exactly what you needed, but it’s quite close.

I tried the redirecting output, but that didn’t work for me. Maybe because the process was writing to a socket, I don’t know.

There are a few options here. One is to redirect the output of the command to a file, and then use ‘tail’ to view new lines that are added to that file in real time.

Another option is to launch your program inside of ‘screen’, which is a sort-of text-based Terminal application. Screen sessions can be attached and detached, but are nominally meant only to be used by the same user, so if you want to share them between users, it’s a big pain in the ass.

For me, this worked:

Login as the owner of the process (even root is denied permission)

Tail the file descriptor as mentioned in many other answers.

You can use reptyr :

How would I ‘attach’ a console/terminal-view to an applications output so I can see what it may be saying?

About this question, I know it is possible to catch the output, even when you didn’t launch sceen command before launching the processus.

While I never tried it, I’ve found an interesting article which explains how to do using GDB (and without restarting your process).

  1. Check the open files list for your process, thanks to /proc/xxx/fd
  2. Attach your process with GDB
  3. While it is paused, close the file you are interested in, calling close() function (you can any function of your process in GDB. I suspect you need debug symbols in your process..)
  4. Open the a new file calling the create() or open() function. (Have a look in comments at the end, you’ll see people suggest to use dup2() to ensure the same handle will be in use)
  5. Detach the process and let in run.

By the way, if you are running a linux OS on i386 box, comments are talking about a better tool to redirect output to a new console : ‘retty’ . If so, consider its use.

Источник

Оцените статью