Skip to content

Instantly share code, notes, and snippets.

@gcamp806
Last active October 24, 2018 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gcamp806/acb9a11e79b707bc03d9684666d79d3f to your computer and use it in GitHub Desktop.
Save gcamp806/acb9a11e79b707bc03d9684666d79d3f to your computer and use it in GitHub Desktop.
Script to add timestamp to every line of a log file

How to add a timestamp to every line of a log file

There are occasions where you would like to have a log file include a timestamp for each line. The following script will allow you to do just that.

timestamp.sh

#!/bin/bash
while read x; do
    echo -n `date +\[%Y-%m-%d\ %H:%M:%S.%3N]`;
    echo -n " ";
    echo $x;
done

sample cron usage

... /path/to/command 2>&1 | /path/to/timestamp.sh >> /var/log/command.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment