Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

See stdin/stdout/stderr of a running process - Linux kernel

Is there a way to redirect/see the stdin/stdout/stderr of a given running process(By PID) in a simple way ?

I tried the following (Assume that 'pid' contains a running user process):

int foo(const void* data, struct file* file, unsigned fd)
{
    printf("Fd = %x\n", fd);
    return 0;
}
struct task_struct* task = pid_task(find_vpid(pid), PIDTYPE_PID);
struct files_struct* fs = task->files;
iterate_fd(fs, 0, foo, NULL);

I get 3 calls to foo (This process probably has 3 opened files, makes sense) but I can't really read from them (from the file pointers).

It prints:

0
1
2

Is it possible to achieve what I asked for in a fairly simple way ?

thanks

like image 809
Rouki Avatar asked Oct 27 '25 22:10

Rouki


1 Answers

First, if you can change your architecure, you run it under something like screen, tmux, nohup, or dtach which will make your life easier.

But if you have a running program, you can use strace to monitor it's kernel calls, including all reads/writes. You will need to limit what it sees (try -e), and maybe filter the output for just the first 3 FDs. Also add -s because the default is to limit the size of data recorded. Something like: strace -p <PID> -e read,write -s 1000000

like image 144
BraveNewCurrency Avatar answered Oct 29 '25 14:10

BraveNewCurrency