每次cron作业运行时都有新的日志文件 [英] New log file every time cron job runs
问题描述
我已经弄清楚了如何使用>>
将cron作业输出到日志文件并提供日志文件的路径. >>
将日志信息附加到现有文件.我该如何做,以便cron作业每次运行时都创建一个新的日志文件? (即rsync.log
,rsync(1).log
,rsync(2).log
-理想情况下,我希望日志的文件名类似于DD-MM-YY.log
).
I have figured out how to output cron jobs to a log file using >>
and providing a path to the log file. The >>
appends log information to the existing file. How can I make it so the cron job creates a new log file every time it runs? (i.e. rsync.log
, rsync(1).log
, rsync(2).log
-- ideally though I'd like the file name of the log to be something like DD-MM-YY.log
).
我想要一个单独的日志文件,因此该日志文件不会太大,以及是否/当我们查看文件/文件夹是否已成功备份时(我在cron作业中运行rsync命令)我们不必梳理MASSIVE日志文件.
I want a separate log file so this one log file doesn't get so huge and if/when we go to look if a file/folder was successfully backed up (I'm running an rsync command in the cron job) we don't have to comb through a MASSIVE log file.
此外,将cron作业输出到日志文件时,没有时间/日期引用.我的第一个日志输出示例:
Additionally, when the cron job is outputted to the log file, there are no time/date references. Example of my first log output:
**sending incremental file list**
**sent 78 bytes received 11 bytes 35.60 bytes/sec**
**total size is 0 speedup is 0.00**
就是这样.每次运行作业时,此日志文件上的时间戳都会不断更改,因此我们甚至无法得知通过rsync复制特定文件/文件夹的日期.如果每次cron作业运行时都有一个单独的日志文件,则可以打开创建该日志文件的特定日期的日志文件,并查看备份的内容.
That's it. The timestamp on this logfile will keep changing every time the job is run, so we wouldn't even be able to tell what day that particular file/folder was copied via rsync. If I had a separate log file for every time the cron job ran, I could just open the log file for the particular date that it was created and view what was backed up.
我当前的cronjob:
My current cronjob:
*/1 * * * * rsync -avz /home/me/test/ 1234@xxx-a123.rsync.net:test/ >>/home/me/cron_logs/homedir_backups/rsync.log 2>&1
出于测试目的,我仅将其设置为1分钟.最终,该操作只会每天在午夜运行.
I only have it set to 1 minute for testing purposes. Eventually this will only run daily at midnight.
推荐答案
如果您希望使用时间戳记日志,请添加格式化日期命令的字符串, 日期+%d%y%m表示日期月份的年份,数字格式.
If you want time stamped logs, you add the string of a formatted date command, date +%d%y%m for day month year in number format.
您可以使用反引号将字符串放入cron:
You can use backtick to put the string in the cron:
~$ /home/me/cron_log-`/bin/date +%d-%m-%y`
因此,文件名将带有当前日期.反引号表示运行此命令并将输出作为字符串放在这里".
So the file name will have the current date appended to it. The backtick says "run this command and put the output here as a string".
现在的问题是您的目录可能会很大,然后您必须编写一个简短的脚本来按时间删除它们.我有一个脚本,可以读取格式并保留X并删除其余的,但是大多数人只会使用"find"按时间删除较旧的内容,例如mtime> 1年的日志.
Now the problem is that your directory might get huge and then you have to write a short script to delete them by time. I have a script that reads the format and keeps X and deletes the rest but most people would just use "find" to delete by time older stuff, like logs that have mtime > 1 year.
这篇关于每次cron作业运行时都有新的日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!