IT story

이미 실행중인 프로세스의 출력을 리디렉션하는 방법

hot-time 2020. 6. 28. 18:06
반응형

이미 실행중인 프로세스의 출력을 리디렉션하는 방법


일반적으로 다음과 같은 명령을 시작합니다

longcommand &;

나는 당신이 같은 것을함으로써 그것을 리디렉션 할 수 있다는 것을 알고있다.

longcommand > /dev/null;

예를 들어 출력을 제거하거나

longcommand 2>&1 > output.log

출력을 캡처합니다.

그러나 나는 때때로 잊어 버렸고 사실 후에 캡처하거나 리디렉션 할 수있는 방법이 있는지 궁금했습니다.

longcommand
ctrl-z
bg 2>&1 > /dev/null

터미널에 메시지가 표시되지 않고 터미널을 계속 사용할 수 있습니다.


실행중인 프로세스에서 출력 리디렉션을 참조하십시오 .

먼저 cat > foo1한 세션 에서 명령 실행하고 stdin의 데이터가 파일로 복사되는지 테스트합니다. 그런 다음 다른 세션에서 출력을 리디렉션합니다.

먼저 프로세스의 PID를 찾으십시오.

$ ps aux | grep cat
rjc 6760 0.0 0.0 1580 376 pts/5 S+ 15:31 0:00 cat

이제 파일 핸들이 열려 있는지 확인하십시오.

$ ls -l /proc/6760/fd
total 3
lrwx—— 1 rjc rjc 64 Feb 27 15:32 0 -> /dev/pts/5
l-wx—— 1 rjc rjc 64 Feb 27 15:32 1 -> /tmp/foo1
lrwx—— 1 rjc rjc 64 Feb 27 15:32 2 -> /dev/pts/5

이제 GDB를 실행하십시오.

$ gdb -p 6760 /bin/cat
GNU gdb 6.4.90-debian

[license stuff snipped]

Attaching to program: /bin/cat, process 6760

[snip other stuff that's not interesting now]

(gdb) p close(1)
$1 = 0
(gdb) p creat("/tmp/foo3", 0600)
$2 = 1
(gdb) q
The program is running. Quit anyway (and detach it)? (y or n) y
Detaching from program: /bin/cat, process 6760

pGDB 명령은 표현식의 값을 출력하고, 표현식은 호출하는 함수가 될 수 있으며, 시스템 호출이 될 수 있습니다 ... 그래서 close()시스템 호출을 실행하고 파일 핸들 1을 전달한 다음 creat()시스템 호출을 실행 하여 새로운 파일. 의 결과 creat()는 1이며 이는 이전 파일 핸들을 대체했음을 의미합니다. stdout 및 stderr에 동일한 파일을 사용하거나 파일 핸들을 다른 숫자로 바꾸려면 dup2()시스템 호출을 호출하여 해당 결과를 얻습니다.

이 예제 에서는 매개 변수가 적기 때문에 creat()대신 사용하기로 선택했습니다 open(). 플래그에 대한 C 매크로는 GDB에서 사용할 수 없으므로 (C 헤더를 사용하지 않음)이를 발견하기 위해 헤더 파일을 읽어야합니다. 그렇게하기는 어렵지 않지만 시간이 더 오래 걸립니다. 0600은 읽기 / 쓰기 액세스 권한이있는 소유자와 그룹 및 액세스 권한이없는 다른 사람에 대한 8 진 권한입니다. 해당 매개 변수에 0을 사용하고 나중에 파일에서 chmod를 실행하는 것이 좋습니다.

그 후 결과를 확인합니다.

ls -l /proc/6760/fd/
total 3
lrwx—— 1 rjc rjc 64 2008-02-27 15:32 0 -> /dev/pts/5
l-wx—— 1 rjc rjc 64 2008-02-27 15:32 1 -> /tmp/foo3 <====
lrwx—— 1 rjc rjc 64 2008-02-27 15:32 2 -> /dev/pts/5

더 많은 데이터를 입력 cat하면 파일 /tmp/foo3이 추가됩니다.

원래 세션을 닫으려면 모든 파일 핸들을 닫고 제어 tty가 될 수있는 새 장치를 연 다음을 호출해야 setsid()합니다.


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

Type

reredirect -m FILE PID

and outputs (standard and error) will be written in FILE.

reredirect README also explains how to restore original state of process, how to redirect to another command or to redirect only stdout or stderr.

reredirect also provide a script called relink that allows to redirect to current terminal:

relink PID
relink PID | grep usefull_content

(reredirect seems to have same features than Dupx described in another answer but, it does not depends on Gdb).


Dupx

Dupx is a simple *nix utility to redirect standard output/input/error of an already running process.

Motivation

I've often found myself in a situation where a process I started on a remote system via SSH takes much longer than I had anticipated. I need to break the SSH connection, but if I do so, the process will die if it tries to write something on stdout/error of a broken pipe. I wish I could suspend the process with ^Z and then do a

bg %1 >/tmp/stdout 2>/tmp/stderr 

Unfortunately this will not work (in shells I know).

http://www.isi.edu/~yuri/dupx/


Screen

If process is running in a screen session you can use screen's log command to log the output of that window to a file:

Switch to the script's window, C-a H to log.
Now you can :

$ tail -f screenlog.2 | grep whatever

From screen's man page:

log [on|off]

Start/stop writing output of the current window to a file "screenlog.n" in the window's default directory, where n is the number of the current window. This filename can be changed with the 'logfile' command. If no parameter is given, the state of logging is toggled. The session log is appended to the previous contents of the file if it already exists. The current contents and the contents of the scrollback history are not included in the session log. Default is 'off'.

I'm sure tmux has something similar as well.


I collected some information on the internet and prepared the script that requires no external tool: See my response here. Hope it's helpful.

참고URL : https://stackoverflow.com/questions/1323956/how-to-redirect-output-of-an-already-running-process

반응형