Skip to content

Instantly share code, notes, and snippets.

@maxwelleite
Last active August 2, 2022 08:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxwelleite/7f63149094a22e75ce6072946acbf3b2 to your computer and use it in GitHub Desktop.
Save maxwelleite/7f63149094a22e75ce6072946acbf3b2 to your computer and use it in GitHub Desktop.
timestampit - add a timestamp to command output in Linux
#!/bin/bash
# Author: Maxwel Leite
# Website: http://needforbits.wordpress.com/
# Initial Author: Abdullah Diab (http://mpcabd.xyz/adding-a-timestamp-to-command-output-in-linux/)
# Description: Simple bash script to add a timestamp to command output in Linux
# This is just a quick solution to put a timestamp with each line of the output of some command in Linux.
while read x; do
# RFC-3339 format - for more accuracy and works with syslog
echo -n `date --rfc-3339=ns | sed 's/ /T/; s/\(\....\).*-/\1-/g'`;
echo -n " ";
echo $x;
done
@maxwelleite
Copy link
Author

maxwelleite commented Sep 5, 2017

Usage:
echo "My script for logging" | /usr/bin/timestampit.sh >> /var/log/cron/my_logging.log 2>&1

Sample output:
2017-09-25T15:35:21.808-03:00 My script for logging

Or usage for a cron job:
/path/to/my/command.sh | /usr/bin/timestampit.sh >> /var/log/cron/my_command.log 2>&1

Install:

sudo wget https://gist.githubusercontent.com/maxwelleite/7f63149094a22e75ce6072946acbf3b2/raw/timestampit.sh -O /usr/bin/timestampit.sh
sudo chmod +x /usr/bin/timestampit.sh

Enjoy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment