Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

view output of already running processes in linux

Tags:

linux

process

ps

I have a process that is running in the background (sh script) and I wonder if it is possible to view the output of this process without having to interrupt it.

The process ran by some application otherwise I would have attached it to a screen for later viewing. It might take an hour to finish and i want to make sure it's running normally with no errors.

like image 310
AL-Kateb Avatar asked Jan 06 '13 17:01

AL-Kateb


People also ask

How do you check the output of a process?

Observing the Output via /proc The subdirectories are named after the process IDs. The /fd folder after process ID is the file descriptor. It logs the streams for the processes, i.e., stdout, stderr, and other outputs. stdout is the output stream of our interest which is /fd file 1.

How do I see current processes running?

You need to use the ps command. It provides information about the currently running processes, including their process identification numbers (PIDs). Both Linux and UNIX support the ps command to display information about all running process. The ps command gives a snapshot of the current processes.

How do I see PID running processes?

The easiest way to find out if process is running is run ps aux command and grep process name. If you got output along with process name/pid, your process is running.

Which command is used to view running processes?

You can use the ps command to find out which processes are running and display information about those processes. The ps command has several flags that enable you to specify which processes to list and what information to display about each process.


2 Answers

There is already an program that uses ptrace(2) in linux to do this, retty:

http://pasky.or.cz/dev/retty/

It works if your running program is already attached to a tty, I do not know if it will work if you run your program in background.

At least it may give some good hints. :)

You can probably retreive the exit code from the program using ptrace(2), otherwise just attach to the process using gdb -p <pid>, and it will be printed when the program dies.

You can also manipulate file descriptors using gdb:

(gdb) p close(1)
$1 = 0
(gdb) p creat("/tmp/stdout", 0600)
$2 = 1

http://etbe.coker.com.au/2008/02/27/redirecting-output-from-a-running-process/

like image 95
emil Avatar answered Oct 19 '22 18:10

emil


You could try to hook into the /proc/[pid]/fd/[012] triple, but likely that won't work.

Next idea that pops to my mind is strace -p [pid], but you'll get "prittified" output. The possible solution is to strace yourself by writing a tiny program using ptrace(2) to hook into write(2) and writing the data somewhere. It will work but is not done in just a few seconds, especially if you're not used to C programming.

Unfortunately I can't think of a program that does precisely what you want, which is why I give you a hint of how to write it yourself. Good luck!

like image 36
gustaf r Avatar answered Oct 19 '22 18:10

gustaf r