My blog is moving to http://victormendonca.com/blog/. If you are looking for a specific or older post you are in the right place Otherwise check out my new page for more up to date content.
Friday, December 23, 2016
Redirecting Bash xtrace to a separate log
So you have a Bash script that you want to troubleshoot, but you want to send stdout to a file, and stderr to another. Here's a solution.
For example, I like to use Bash's color to display failure or success on checks, and echo's removal of new lines (echo -e "Wait for it...\c ") to wait for checks. For example, the screenshot below shows a script that check each step and report back.
Errors are displayed in red, and success in green, and this same output is sent to the stdout and to a log file as well.
Now I want to get error messages in a separate log file, so I can go back to my original log and match the time where it failed, with additional verbose from Bash's xtrace.
The solution is very simple... I can just add the lines below to the top of my script:
The first line setups my debug log, the second redirects stderr to my debug log, and the third enables xtrace (same as bash -x [script]).
Note that doing this will disable any errors from being show in stdout.