Linux get process stdout

See the STDOUT redirect of a running process

Check out the file descriptor #1 (STDOUT) in /proc/$PID/fd/ . The kernel represents this file as symbolic link to a file the descriptor is redirected to.

$ readlink -f /proc/20361/fd/1 /tmp/file 

Perfect! Thanks! Can you provide a link or details about the other redirects such as STDERR, ie which numbers they are?

At least on Linux (and I believe on every other known UNIX-like OS) they are 0: stdin, 1: stdout, 2: stderr (standard error). Note there are also C macros defined in : STD_FILENO. See ‘man stdout’ for details.

0, 1, 2 for stdin, stdout and stderr respectively are guaranteed in all Unix/Unix-like systems, and are also true on Windows.

on MacOS any way to do this? the /proc//fd/1 does not exist, maybe I can use mkfifo?

A lot of answers mention doing it this way:

However, I’ve found that it doesn’t always work. Alternatively, the cat sometimes yields results.

where can be looked up using the ps aux command.

Also good to mention, is that the number on the end of the command (in this case fd/1) can be changed for other outputs.

 /proc//fd/0 # STDIN /proc//fd/1 # STDOUT /proc//fd/2 # STDERR 

A useful tool to see what files are opened by what processes is lsof . You can point it at a specific process with lsof -p1234 , and you’ll see mostly the same information as you can get with ls -l /proc/1234/fd under Linux, i.e. what files are opened.

The most useful thing with lsof is going the other way round: lsof /path/to/file tells you what processes are using that file.

You must log in to answer this question.

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

Linux is a registered trademark of Linus Torvalds. UNIX is a registered trademark of The Open Group.
This site is not affiliated with Linus Torvalds or The Open Group in any way.

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Read stdout from a process (linux embedded)

You state, that your process will be respnsible to start/stop that server process — THIS makes things a lot easier 🙂 As this is homework, I’ll just draw the picture: create named pipes for ther server’s stdin and stdout when starting the server, connect its stdin/stdout with the named pipes when starting your client, read/write from/to the named pipes Solution 3: Do you have the option of configuring the server so that it sends output to a log file instead, or in addition to, stdout? However, the monitor can be closed/reopened and should hook to existent server stdout to read errors that are written on stdout (and also some output based on some monitor requests).

Читайте также:  Astra linux домен недоступен

Read stdout from a process (linux embedded)

Before flagging the question as duplicate, please read my various issues I encountered.

A bit of background: we are developing a C++ application running on embedded ARM sbc using a lite variant of debian linux. The application start at boot launched by the boot script and print various information to stdout. What we would like is the ability to connect using SSH/Telnet and read the application output, without having to kill the process and restart it for the current bash session. I want to create a simple .sh script for non-tech-savvy people to use.

The first solution for the similar question posted here is to use gdb. First it’s not user-friendly (need to write multiple commands manually) and I wonder why but it don’t seems to output anything into the file.

The second solution strace -ewrite -p PID works perfectly, that’s what I want. Problem is, there’s a lot more information than just the stdout, and it’s badly formatted.

I managed to get an «acceptable» result with strace -e write=1 -s 1024 -p 20049 2>&1 | grep «write(1,» but it still have the superfluous write(1, «. «, 19) = 19 text. Up to this point it’s simply a bit of string formatting, and I’ve found on multiple other pages this line saying it achieve good formatting : strace -ff -e write=1,2 -s 1024 -p PID 2>&1 | grep «^ |» | cut -c11-60 | sed -e ‘s/ //g’ | xxd -r -p

There are some things I find strange in this command (why -ff?, why grep «^ |»?, why use xxd there?) and it just don’t output anything when I try it.

Unfortunately, we do use a old buggy version of busybox (1.7.1) that have some problem with multiple pipes. That bug gives me bad results. For example, if I only do grep it works, and if I only do cut it also works, but let’s say «grep «write(1,» | cut -c11-60″ returns nothing.

I know the real solution would simply be to update busybox and use these multiple pipes to format the string, but we can’t update it since the os distribution is already installed on thousands of boards shipped to our clients worldwide..

Anyone have a miraculous solution? Thanks

Screen can be connected to an existing process using reptyr (http://blog.nelhage.com/2011/01/reptyr-attach-a-running-process-to-a-new-terminal/), or you can use neercs (http://caca.zoy.org/wiki/neercs) which I haven’t used but apparently is like screen but supports attaching to an existing process all by itself.

Shell — See the STDOUT redirect of a running process, A useful tool to see what files are opened by what processes is lsof.You can point it at a specific process with lsof -p1234, and you’ll see mostly the same information as you can get with ls -l /proc/1234/fd under Linux, i.e. what files are opened.. The most useful thing with lsof is going the other way round: lsof /path/to/file tells …

Читайте также:  Linux hostname and fqdn

Attach a program to a process stdout/stderr

I have a program where I need to attach to stdout/stderr of a given pid (fetched from a file). How this should be done?Is this even possible?

I have a monitor program that starts/stops a server program. However, the monitor can be closed/reopened and should hook to existent server stdout to read errors that are written on stdout (and also some output based on some monitor requests).

I built server and monitor so I have sources of both, the problem is that the server «answer» to some monitor requests on the stdout, I don’t want to add another interprocess comunication part

While a process is running, there isn’t a standard Unix way to intercept its output from another process and start capturing it after the target process has been started.

If you are starting these processes yourself, via execve , you can simply set up a pipe via pipe(2) and redirect its descriptors (via dup2(2) ) to the child process’ stdin and stdout. This way the parent will be able to write/read to the child’s stdin/stdout through the pipe.

Regarding your question after the edit: this seems like a good fit for a Unix fifo file.

A fifo file (or a named pipe) appears like a file, but is implemented as a pipe under the bonnet.

So just create a fifo file (with the mkfifo(1) command), start the server application by redirecting its stdin and stdout descriptors to that file (with the < and >operators of the shell), and you’ll be able to read from it anytime.

Never tried, but you may look at the /proc/$pid/ directory if it is possible (with proper permissions) to attach to the file descriptor entries there. Otherwise I couldn’t imagine how this would be possible.

EDIT (after getting more details)

You state, that your process will be respnsible to start/stop that server process — THIS makes things a lot easier 🙂

As this is homework, I’ll just draw the picture:

  • create named pipes for ther server’s stdin and stdout
  • when starting the server, connect its stdin/stdout with the named pipes
  • when starting your client, read/write from/to the named pipes

Do you have the option of configuring the server so that it sends output to a log file instead, or in addition to, stdout? On a unix box, you could run the server through tee to log stdout to a file:

Then it is a simple matter to tail server.log to get the latest output.

Get stdout of already running process in Linux in C, Since you have the PID (returned from posix_spawn) and you are running Linux, you will find the stdout of the process at /proc//fd/1. Just open (or fopen) the file for reading. The standard way is to use fork though. Use pipe and dup2 to get a file descriptor for reading the child’s output, as in this question.

How to run process which only attach stdin, stdout and stderr to another running process

I have application which spawns child process ans used stdin/stdout/stderr to communicate with it. I can modify a spawn command.

What I would like to achieve is run this child process manually before spawn from application (eg because I want to run in debugger) and attach to it with standard spawn.

Читайте также:  Можно линукс заменить виндовс

Is it possible? I know there is /proc/PID/fd/0 and so on. But I still don’t know if it’s possible to connect it by spawning another «mock process» to it.

Attach as a debugger to the child process. Create new pipes for stdin, stdout, and stderr, and use dup2 to put them in place. If you’re on Linux 5.6 or newer, use pidfd_getfd to get the other ends of these pipes into your other process. Otherwise, send them as SCM_RIGHTS ancillary data over a Unix domain socket to your other process.

Run any Linux terminal command from TypeScript?, Is there a way to execute a Linux terminal command directly from within a TypeScript class? The idea is to do something similar to: let myTerminal = new LinuxTerminal(); let terminalResult = myTerminal.run(«sudo apt-get update»); EDIT: I run this through Node JS with Angular. Code sampleconst < exec >= require(‘child_process’);exec(‘sudo apt-get update’, (err, stdout, stderr) => );Feedback

How to change the output redirection of a running process?

I know how to redirect output and how to suppress them in bash. Now, suppose I accidentally forgot to append the output redirection part to the command (e.g. 2>&1 or > /tmp/mystdout ) and my background process is already running for a while, can I still change to where stdout and stderr are being written to? I really would like not to kill and restart the application.

To be more specific as asked by Gilles in his comment, I would like to fiddle with it in these scenarios in specific:

E.g. I have Apache running and I can see the file descriptors:

/proc/8019/fd/0 -> /dev/null /proc/8019/fd/1 -> /dev/null /proc/8019/fd/2 -> /var/log/apache2/error.log 

You can do it using reredirect (https://github.com/jerome-pouiller/reredirect/).

You can restore initial output of your process later using something like:

( and are provided by previous launch of reredirect).

reredirect README also explains how to redirect to another command or to redirect only stdout or stderr.

That’s not possible. Or at least not easy. You might have some luck attaching gdb to the process and fiddling with it in gdb, but that can lead to crashes just as easily as success.

You should use reptyr to attach another process to your new current terminal. For example: install and use GNU’s screen utility, and in it launch the reptyr command to «fetch» the other process’s redirections (and controlling terminal) and attach them to the new current terminal (the screen process, if you launched reptyr from screen ). The advantage of doing this from a screen session is that, once under screen , it will then be easy to disconnect and reconnect to it (for example, closing your local session, going home, and reopening it).

Read stdout from a process (linux embedded), A bit of background: we are developing a C++ application running on embedded ARM sbc using a lite variant of debian linux. The application start at boot launched by the boot script and print various information to stdout. What we would like is the ability to connect using SSH/Telnet and read the application …

Источник

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