From be7314bceec2e05c74997df98908a07bcf7b7a64 Mon Sep 17 00:00:00 2001 From: Andreas Hrubak Date: Tue, 4 Feb 2025 22:06:28 +0100 Subject: [PATCH] stdfilt to access child process pid --- user-tools/stdfilt | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/user-tools/stdfilt b/user-tools/stdfilt index 8b329fc..2b00734 100755 --- a/user-tools/stdfilt +++ b/user-tools/stdfilt @@ -261,9 +261,9 @@ pipe($stdout_r, $stdout_w) or die "$0: pipe: $!\n"; pipe($stderr_r, $stderr_w) or die "$0: pipe: $!\n"; -my $child_pid = fork // die "$0: fork: $!\n"; +$CHILD_PID = fork // die "$0: fork: $!\n"; -if($child_pid == 0) +if($CHILD_PID == 0) { open STDOUT, '>&', $stdout_w or die "$0: replace stdout: $!\n"; open STDERR, '>&', $stderr_w or die "$0: replace stderr: $!\n"; @@ -363,7 +363,7 @@ while(1) } } -waitpid($child_pid, 0); +waitpid($CHILD_PID, 0); $child_status = $?; $exit_status = WEXITSTATUS($child_status); $exit_status = 128 + WTERMSIG($child_status) if WIFSIGNALED($child_status); @@ -499,6 +499,16 @@ This prepends a TAB char to each lines in the output stream which are between th HUP - re-read filter files given at command line +=head1 EXAMPLES + +Prefix each output (and stderr) lines with the I process'es PID: + + stdfilt -f 's/^/$CHILD_PID: /' some_command... + +Prefix each line with STDOUT/STDERR: + + stdfile -f '[STDOUT]' -f 's/^/STDOUT: /' -f '[/STDOUT]' -f '[STDERR]' -f 's/^/STDERR: /' -f '[/STDERR]' some_command... + =head1 SEE ALSO grep(1), stdbuf(1), logwall(8), perlre(1) -- 2.11.4.GIT