Adding time stamp to log file in bash script

Note you are outputting to Debug.log, while you should indicate the full path of that file: echo "Time: $(date)" >> /path/to/Debug.log.


In general, whenever you want to add timestamp to a log file, you can use:

echo "Time: $(date). Some error info." >> /path/to/your/file.log

date will expand to something like Fri Sep 9 12:18:02 CEST 2016. In that case, you may prefer to use some date flags to have a more parseable date:

$ date "+%FT%T"
2016-09-09T12:18:23

Or, even better, using the ISO 8601 format:

$ date -Iseconds
2016-09-09T12:18:23+0200

All together:

echo "Time: $(date -Iseconds). Some error info." >> /path/to/your/file.log

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)