Can one pick up a running application from terminal?

Sometime I need to run an application from terminal for debugging. If I am sure the bug will occur short after launching the application, I can run this application from the Terminal.

However, bugs occur unexpectedly, and then only I need to monitor the buggy application from the Terminal (to see its output).

Can I then pick up an application from terminal, which was not launched using Terminal? If so how?

2

4 Answers

Each process in linux has a special directory /proc/{pid}/fd/. 0 is stdin, 1 is stdout and 2 is stderr. So, assuming you are only interested in diagnostic output you can determine the process pid, and then in the terminal do:

to see stdout:

cat /proc/{pid of process}/fd/1

to see stderr:

cat /proc/{pid of process}/fd/2
1

Or you can use strace like this

sudo strace -p $pid_of_the_process
0

You can attach with gdb to a running process.

The syntax is

gdb program pid

Ok, you cannot see source code, if debug information are stripped, which is the default for deployed applications. But you can probably see stdout/stderr and debugger messages, segfaults.

9

You are likely looking for retty. You could also look for "attach tty". Basically it's done using ptrace so you could even roll your own.

0

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like