Last active
August 2, 2022 08:12
-
-
Save maxwelleite/7f63149094a22e75ce6072946acbf3b2 to your computer and use it in GitHub Desktop.
timestampit - add a timestamp to command output in Linux
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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:
Enjoy!