logrotate 配置文件中是否支持变量?
我查看了 logrotate.conf 示例以及 /etc/logrotate.d 目录中的所有内容。我无法在这些文件中找到有关变量的文档。
我正在尝试创建一个配置文件来轮换我们正在编写的应用程序的日志。我想设置一次存储日志的目录,然后将其用作变量,如下所示:
my_app_log_dir=/where/it/is/deployed/logs
${my_app_log_dir}/*.log ${my_app_log_dir}/some_sub_dir/*.log {
missingok
# and so on
# ...
}
可以吗?
I looked at logrotate.conf examples and everything in my /etc/logrotate.d directory. Nowhere was I able to find documentation on variables in these files.
I am trying to create a config file for rotating the logs of an application we are writing. I want to set the directory where logs are stored once, and then use it as a variable, like so:
my_app_log_dir=/where/it/is/deployed/logs
${my_app_log_dir}/*.log ${my_app_log_dir}/some_sub_dir/*.log {
missingok
# and so on
# ...
}
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以通过实现此组合来实现您正在寻找的内容:
my-logrotate.conf(注意:双引号
"
是强制性的,另请注意,文件名不必出现在同一行)然后是实际的 logrotate 脚本 - my-logrotate.sh
现在您可以将 logrotate.sh 添加到您的 crontab 中。
You can achieve what you are looking for by implementing this kludge:
my-logrotate.conf ( NOTE: double quotes
"
are mandatory, also note that file names don't have to appear on the same line )Then the actual logrotate script - my-logrotate.sh
Now you can add logrotate.sh to your crontab.
您可以使用 bash“此处文档" 创建合适的配置文件即时执行,无论是在安装时还是在运行 logrotate 之前。
bash 脚本可能如下所示:
You can use a bash "here document" to create a suitable config file on the fly, either at installation time or before running logrotate.
A bash script might look like this:
直接在配置文件中没有(据我对 logrotate 的了解)。
其他解决方案:
Directly in the config file no (as far as my knowledge in logrotate goes).
Other solution:
在 logrotate 3.8.7 中,测试表明您可以在预旋转和后旋转脚本部分中设置和使用变量。
我针对特定的服务日志文件尝试了此操作。
在强制模式下运行 logrotate 以确保日志文件已轮换并执行后轮换脚本后,查看 /run/some_service 时,发现有一个附加文件“some_serviced.pid.WAS_USED”,从而证明该变量的使用工作了。
With logrotate 3.8.7,a test reveals that you can set and use variables in the pre-rotate and post-rotate script sections.
I tried this for a particular service log file.
After running logrotate in force mode to ensure the log file was rotated and the post-rotate script executed, on looking in /run/some_service, there was an additional file "some_serviced.pid.WAS_USED", thus proving that the use of the variable worked.
另一种选择(可能不是最干净的)是在 logrotate.conf 文件中放置一个占位符,可以在安装时将其替换为真实目录。
logrotate.conf
:那么你的installation.sh中就会有这样的内容:
如果权限是一个问题,你可以选择不将logrotate.conf放在/etc/logrotate.d中,而是放在任何其他目录。在这种情况下,您必须自己调用 logrotate,并指定您的 conf 文件:
Another option - probably not the cleanest - is to have a placeholder in the
logrotate.conf
file, which can be replaced on installation time for the real directory.logrotate.conf
:Then you would have in your installation.sh something like:
If permissions are an issue, you can choose not to put the logrotate.conf in /etc/logrotate.d, but in any other directory. In such case you would have to call logrotate yourself, specifying your conf file: