Redirecting standard output to a file containing the pid of the logging process [duplicate]
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.
精彩评论