Reputation: 127
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
Upvotes: 6
Views: 16337
Reputation: 11
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
Upvotes: 0
Reputation: 246837
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.
Upvotes: 8