开发者

Redirecting standard output to a file containing the pid of the logging process [duplicate]

This question already has answers here: Redirecting standard output to a file containing the pid of the logging process (2 answers) Closed 2 years ago.

I've searched for a while but i can't either find an answer or come up with a solution of my own, so I turn to you guys. First question I actually ask here :)

I would like to run several instances of the same program, and redirect each of these programs' standard output to a file that contains that same process' pid, something like:

my_program > <pid of the instance of my_program that is called in this command>.log

I'm aware 开发者_Go百科that this is not even close of the way to go :P I have tinkered around with exec and $PPID but to no avail. My bash-fu is weak :| please help me, point me somewhere! Thanks!


You can wrap your program execution into bash script. The bash process will be replaced with your program on exec call. So:

#!/bin/bash
exec my_program > $$.log


You cannot know the PID of a process before you created it.

Therefore this is not possible, you should rewrite the program that is called, to use getpid() to forge a log name from its own PID.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜