logrotate 配置文件中是否支持变量?

发布于 2024-08-11 11:05:47 字数 332 浏览 6 评论 0原文

我查看了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

迷迭香的记忆 2024-08-18 11:05:47

您可以通过实现此组合来实现您正在寻找的内容:

my-logrotate.conf(注意:双引号 " 是强制性的,另请注意,文件名不必出现在同一行)

"*.log"
"some_sub_dir/*.log"
{
    missingok
    # and so on
    # ...
}

然后是实际的 logrotate 脚本 - my-logrotate.sh

#!/bin/sh

set -eu

cd "${my_app_log_dir}"
exec logrotate /path/to/my-logrotate.conf

现在您可以将 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 )

"*.log"
"some_sub_dir/*.log"
{
    missingok
    # and so on
    # ...
}

Then the actual logrotate script - my-logrotate.sh

#!/bin/sh

set -eu

cd "${my_app_log_dir}"
exec logrotate /path/to/my-logrotate.conf

Now you can add logrotate.sh to your crontab.

柠檬色的秋千 2024-08-18 11:05:47

您可以使用 bash“此处文档" 创建合适的配置文件即时执行,无论是在安装时还是在运行 logrotate 之前。

bash 脚本可能如下所示:

cat >rt.conf <<.
"${my_app_log_dir}/*.log" {
    rotate 5
    size 15k
    missingok
}
.

logrotate  rt.conf

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:

cat >rt.conf <<.
"${my_app_log_dir}/*.log" {
    rotate 5
    size 15k
    missingok
}
.

logrotate  rt.conf
一个人的旅程 2024-08-18 11:05:47

直接在配置文件中没有(据我对 logrotate 的了解)。

其他解决方案:

  • 使用 include 选项包含目录中的部分配置文件。如果您有应用程序的包,这可以帮助您,该包可以在该目录中留下一个仅包含您的应用程序条目的文件。

Directly in the config file no (as far as my knowledge in logrotate goes).

Other solution:

  • Use the include option to include parts of the configuration file from a directory. This can help you if you have a package for your application, the package can leave a file in that directory containing only the entries for your app.
温柔嚣张 2024-08-18 11:05:47

在 logrotate 3.8.7 中,测试表明您可以在预旋转和后旋转脚本部分中设置和使用变量。

我针对特定的服务日志文件尝试了此操作。

   postrotate
         pid_file="/run/some_service/some_serviced.pid"
         test -e "${pid_file}" && kill -s HUP $(cat "${pid_file}") || true
         touch "${pid_file}.WAS_USED"
   endscript

在强制模式下运行 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.

   postrotate
         pid_file="/run/some_service/some_serviced.pid"
         test -e "${pid_file}" && kill -s HUP $(cat "${pid_file}") || true
         touch "${pid_file}.WAS_USED"
   endscript

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.

千鲤 2024-08-18 11:05:47

另一种选择(可能不是最干净的)是在 logrotate.conf 文件中放置一个占位符,可以在安装时将其替换为真实目录。

logrotate.conf

(...)
XX_APP_LOG_DIR_XX/*.log {
(...)
}

那么你的installation.sh中就会有这样的内容:

sed -i "s/XX_APP_LOG_DIR_XX/$my_app_log_dir/" $CNF_DIR/logrotate.conf

如果权限是一个问题,你可以选择不将logrotate.conf放在/etc/logrotate.d中,而是放在任何其他目录。在这种情况下,您必须自己调用 logrotate,并指定您的 conf 文件:

logrotate $CNF_DIR/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 :

(...)
XX_APP_LOG_DIR_XX/*.log {
(...)
}

Then you would have in your installation.sh something like:

sed -i "s/XX_APP_LOG_DIR_XX/$my_app_log_dir/" $CNF_DIR/logrotate.conf

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:

logrotate $CNF_DIR/logrotate.conf
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文