Jay Taylor's notes
back to listing indexHow to capture ordered STDOUT/STDERR and add timestamp/prefixes?
[web search]
I have explored almost all available similar questions, to no avail. Let me describe the problem in detail: I run some unattended scripts and these can produce standard output and standard error lines, I want to capture them in their precise order as displayed by a terminal emulator and then add a prefix like "STDERR: " and "STDOUT: " to them. I have tried using pipes and even epoll-based approach on them, to no avail. I think solution is in pty usage, although I am no master at that. I have also peeked into the source code of Gnome's VTE, but that has not been much productive. Ideally I would use Go instead of Bash to accomplish this, but I have not been able to. Seems like pipes automatically forbid keeping a correct lines order because of buffering. Has somebody been able to do something similar? Or it is just impossible? I think that if a terminal emulator can do it, then it's not - maybe by creating a small C program handling the PTY(s) differently? Ideally I would use asynchronous input to read these 2 streams (STDOUT and STDERR) and then re-print them second my needs, but order of input is crucial! NOTE: I am aware of stderred but it does not work for me with Bash scripts and cannot be easily edited to add a prefix (since it basically wraps plenty of syscalls). Update: added below two gists (sub-second random delays can be added in sample script I provided to prove a consistent result) Update: solution to this question would also solve this other question, as @Gilles pointed out. However I have come to the conclusion that it's not possible to do what asked here and there. When using I will be eager to update this question if somebody can disproof the above. |
|||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||
You might use coprocesses. Simple wrapper that feeds both outputs of a given command to two
Note several things:
As far as coprocesses be sure to read Stéphane's excellent answer in How do you use the command coproc in Bash? for in depth insight. |
|||||||||||||||||||||||||||||||||
|
Method #1. Using file descriptors and awkWhat about something like this using the solutions from this SO Q&A titled: Is there a Unix utility to prepend timestamps to lines of text? and this SO Q&A titled: pipe STDOUT and STDERR to two different processes in shell script?. The approachStep 1, we create 2 functions in Bash that will perform the timestamp message when called.
Step 2 you'd use the above functions like so to get the desired messaging:
ExampleHere I've concocted an example that will write
Method #2. Using annotate-outputThere's a tool called ExampleIf we put our above example command sequence into a script called
We can then run it like this:
The output's format can be controlled for the timestamp portion but not beyond that. But it's similar output to what you're looking for, so it may fit the bill. |
|||||||||||||||
|
Your Answer
Not the answer you're looking for? Browse other questions tagged / bash / shell / io-redirection / pipe or ask your own question.
asked |
1 year ago |
viewed |
1697 times |
active |
Linked
Related
Hot Network Questions
- Why invest for the long-term rather than buy and sell for quick, big gains?
- Why are airbus captain control sticks placed on the left?
- Refund for cancelling return leg of an itinerary in advance
- Remove space after cases environment
- Is it appropriate to verify non-job-relevant claims on a candidate's resume?
- Was the original Enterprise truly the first sci-fi spaceship designed purely for exploration?
- Does rolling a 1 with Advantage cancel out the higher roll?
- Is there a short phrase to describe forgetting to do something because something else had your attention?
- Why is Steam so insistent on security?
- How do I convince players to play a D&D campaign with a restrictive premise?
- Strange definitions of TRUE and FALSE macros
- To 'this' or not to 'this'?
- Getting files back by paying Ransomeware
- Does adding water back into an over-reduced sauce lose flavour?
- Did we ever see the captain's yacht?
- How to not use a singleton in C++ game engine programming?
- Vim - how to replace one new line \n with two \n's
- What happened to Stan Lee after he drank the gamma soda?
- How can I add integers in an array
- "Last night at 9 PM, I ate dinner" -- Does this sentence mean that it began at 9 or finished at 9?
- How to tell if it's safe to jump an obstacle?
- Why it is called 'Black Friday'?
- Are contemporary chess players now stronger than players in the 90's or 80's?
- compute or prove that a limit does not exist
Technology | Life / Arts | Culture / Recreation | Science | Other | ||
---|---|---|---|---|---|---|
This site is not affiliated with Linus Torvalds or The Open Group in any way.