Posts

Showing posts with the label Timestamps

Redirect stdout / stderr output to log file with timestamp

 To redirect both standard output (stdout) and standard error (stderr) to a log file with timestamps in a Unix-like system, you can use a command like this: ```bash command > >(while IFS= read -r line; do echo "$(date '+%Y-%m-%d %H:%M:%S') $line"; done) 2> >(while IFS= read -r line; do echo "$(date '+%Y-%m-%d %H:%M:%S') $line" >&2; done) | tee -a logfile.log ``` Here's what this command does: 1. `command`: Replace this with the actual command whose output you want to capture. 2. `> >(while IFS= read -r line; do echo "$(date '+%Y-%m-%d %H:%M:%S') $line"; done)`: This redirects stdout to a process substitution, which prefixes each line with a timestamp in the format "YYYY-MM-DD HH:MM:SS". 3. `2> >(while IFS= read -r line; do echo "$(date '+%Y-%m-%d %H:%M:%S') $line" >&2; done)`: This redirects stderr to a process substitution, which also adds timestamps and sends th