apache 如何使用cron为日志输出条目添加时间戳?

yacmzcpb  于 6个月前  发布在  Apache
关注(0)|答案(2)|浏览(65)

我尝试用cron将htcacheclean安排为每半小时运行一次,服务本身运行正常,但我不知道如何获得时间戳来附加到日志条目本身(而不是cron-log文件名)。
关于如何将时间戳添加到日志条目本身,我在任何地方都找不到答案。我只看到关于如何将日期时间戳添加到cron日志文件名的问题和答案,这不是我想要做的。
这就是我现在在crontab -e中所做的:

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

字符串
这是我目前的输出打印方式:

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


现在我只需要一个时间戳出现在旁边的日志条目之前的“统计”。

编辑:

我正在尝试做的事情的例子:

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


我想添加年、月、日、小时、分钟和秒。
提前感谢!

解决方案:

我想按照@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


在cron任务中添加printf命令后,现在我的输出如下所示:

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

vdzxcuhz

vdzxcuhz1#

您可以在cron条目中使用date命令,如下所示:

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
# ...........^.^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^.........................................................................^^^

字符串
printf有点复杂,但它可以像您希望的那样抑制换行符。
编辑以避开百分比符号。参见下面的注解。

eqqqjvef

eqqqjvef2#

我在用这个。

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

#crontab -e
*/30 */1 * * * /bin/bash /somedir/backup.sh

字符串

相关问题