5

I am trying to schedule htcacheclean to run every half hour with cron. The service itself runs fine, but I can't figure out how to get a timestamp to append to the log entries themselves (not the cron-log filename).

I could not quite find an answer anywhere as to how to add the timestamp to log entries themselves. I only kept seeing questions and answers for how to add date timestamps to cron log filenames which is not what I am looking to do.

This is what I have in crontab -e right now:

0,30  *   *   *   *       /usr/sbin/htcacheclean -v -n -t -p/var/cache/apache/mod_disk_cache -l1M >> /path/to/cron-output.log 2>&1

Currently this is how my output is printing:

Statistics:
size limit 1.0M
total size was 167.4K, total size now 167.4K
total entries was 5, total entries now 5

Now I just need a timestamp to appear next to the log entry just before "Statistics".

Edit:

Example of what I'm trying to do:

0000-00-00 00:00:00: Statistics:
size limit 1.0M
total size was 167.4K, total size now 167.4K
total entries was 5, total entries now 5

I want to add year, month, day, hour, minute, and seconds.

Thanks ahead of time!

Solution:

I wanted to add in the full solution here as suggested by @glenn jackman.

0,30  *  *  *  *    { printf "\%s: " "$(date "+\%F \%T")"; /usr/sbin/htcacheclean -v -n -t -p/var/cache/apache/mod_disk_cache -l1M ; } >> /path/to/cron-output.log 2>&1

After adding the printf command in the cron task, this is now what my output looks like:

2018-02-05 21:10:01: Statistics:
size limit 1.0M
total size was 1009.0K, total size now 1009.0K
total entries was 24, total entries now 24
d.r.v.
  • 117
  • 1
  • 1
  • 8
  • 1
    do you want each record to have a uniq time-stamp. or will having the crontab entry time on all records be sufficent? Please update your Q with sample output records. Good luck. – shellter Feb 05 '18 at 19:21
  • @shellter Each entry would have unique time-stamps. Do you mean sample output including the type of time-stamp I want? It would be Y/m/d H/M/S (year, month, day, hour, minute, second) followed by the output of htcacheclean. – d.r.v. Feb 05 '18 at 19:26
  • What will be the source of calculating the time for the log record? Ideally, it should be the program that is generating the log output. And yes, please update your Q with exact output required from your input, including some dummy timestamps. Otherwise, people are likely to misinterpret your requirements. AND does your system have a newish version of `gawk` available. Good luck. – shellter Feb 05 '18 at 19:31
  • Possible duplicate of [Append current date to the filename via Cron?](https://stackoverflow.com/q/9110663/608639), [Sending cron output to a file with a timestamp in its name](https://serverfault.com/q/117360), [How to add the logs to a crontab with time stamp](https://askubuntu.com/q/391542), [How can cron output to a new log file based on date?](https://stackoverflow.com/q/27539083/608639), [How to log cron jobs?](https://stackoverflow.com/q/4811738/608639), etc. – jww Oct 30 '19 at 21:54

2 Answers2

7

You could just use the date command in your cron entry, like this:

0,30 * * * * { printf "\%s: " "$(date "+\%F \%T")"; /usr/sbin/htcacheclean -v -n -t -p/var/cache/apache/mod_disk_cache -l1M ; } >> /path/to/cron-output.log 2>&1
# ...........^.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.........................................................................^^^

The printf is a bit convoluted, but it suppresses the newline like you want.

Edited to escape the percent signs. See comments below.

glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • 1
    Thanks @glenn jackman , this worked. I had to make a slight modification to it by adding backslaches in front of the '%' signs for dates as cron would stop executing at `{ printf " `. This is what worked for me: `{ printf "\%s: " "$(date "+\%F \%T")"; /usr/sbin/htcacheclean -v -n -t -p/var/cache/apache/mod_disk_cache -l1M ; } >> /path/to/cron-output.log 2>&1` – d.r.v. Feb 05 '18 at 21:33
  • Yes, I forgot about that. That's a frequently made mistake in crontabs. – glenn jackman Feb 05 '18 at 21:56
  • Is it possible to use a bashrc function instead, like { print_date(); command } >> logfile? Trying to make cron job shorter. – Edward Apr 15 '18 at 02:52
0

I am using this.

#backup.sh
(date; /usr/bin/rsync -au  /sorcedir/ /dest/dir) &> /somedir/logfile.log

#crontab -e
*/30 */1 * * * /bin/bash /somedir/backup.sh
GK P
  • 11
  • 4