Skip to content

Home > GNU/Linux > Adding timestamps in crontabs

Adding timestamps in crontabs

Wednesday 11 September 2013, by Stéphane Téletchéa

Crontab entries are great to periodically execute a command, but lack advanced logging facilities. This article will just highlight a rapid tip to enhance this situation.

The cron service is meant to execute periodically a given task, but it lacks a proper logging facility.

To add a specific unique logging entry, we will use a variable define inside the crontab command line. Here is the content of my command line


22  6   *   *   1-7 TIMSTP=`date +\%Y\%m\%d-\%H\%M\%S`; $PROGDIR/ 2>&1 > $LOG/${TIMSTP}-update_list.log

The command starting with 22 6 and ending with update_list.log is on the same line.

First we define some variables which are not known by cron (LOG and PROGDIR paths), then we define a TIMSTP (timestamp) variable using the YYYYMMDD-hhmmss format, and execute the program "".

In order to get debugging information if needed, we merge the standard ouput (&>1) and the standard error, and redirect both output in one file, assembled from the LOG variables define for the whoel crontab, and the TMSTP variable, specific to this execution line.

Note that you MUST use this syntax, using the date command out of the crontab execution line will not work.

Comment on this article