JLavoie
JLavoie

Reputation: 17596

How to see the current running Linux processes output^

I have several Linux (Ubuntu 10.04) processes running on my computer. If I start one of them, I can see the verbose of it on my terminal. I have an other process that start a dozen of those processes so that they run in background. However, I would like to watch the output of one of these processes to see if the output is still ok and there are no error message. I know I could send everything into a log message, however, this would just use too much disk space. So, is there a way to "catch" the output of a running process in Linux using it's process Id?

Upvotes: 4

Views: 17330

Answers (3)

Oussama Boumaad
Oussama Boumaad

Reputation: 632

Best way to do is to check fd folder in /proc/<PID>/fd

in my case I have a python running on the system with a different terminal

I can see its logs by :

1. Get Process ID

ps -aux | grep python

enter image description here

2. CD into /proc/504951/fd and view all/needed files

cd /proc/504951/fd && tail -f *

enter image description here

Upvotes: 1

Maria Boghiu
Maria Boghiu

Reputation: 512

You can attach gdb to your running process and redirect stdout/err or both to some log file whenever you feel like looking at the output (make sure the files exist before redirecting):

gdb attach PID
  (gdb) p dup2(open("/tmp/mylogfile-stdout", 0), 1)
  (gdb) p dup2(open("/tmp/mylogfile-stderr", 0), 2)
  (gdb) detach
  (gdb) quit

When you want them to go back to being silent just do:

gdb attach PID
  (gdb) p dup2(open("/dev/null", 0), 1)
  (gdb) p dup2(open("/dev/null", 0), 2)
  (gdb) detach
  (gdb) quit

The 'detach' part is important, otherwise you will kill the process you're attached to when gdb exits. For more details see this question.

Upvotes: 5

Use redirection like

yourprogram arg1 arg2 > yourprog.out

or probably even (to redirect both stderr & stdout and run in the background)

yourprogram arg1 arg2 > yourprog.out 2>&1 &

In a different terminal, do

tail -f yourprog.out 

With the -f option, the tail command will notice when the file is growing and will display its newest lines

But I can't see portable way to redirect after. Maybe screen, batch, at, cron might help you. Or opening the /proc/1234/fd/1 ...

BTW, I am surprised you don't have enough temporary disk space for your output...

And I do like running M-x shell under emacs, and running my programm there.

Upvotes: 1

Related Questions