Are variables supported in logrotate configuration files?
Asked Answered
F

5

10

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?

Femineity answered 19/11, 2009 at 9:9 Comment(0)
T
11

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.

Teferi answered 11/12, 2010 at 14:23 Comment(0)
M
5

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
Majors answered 23/3, 2016 at 9:35 Comment(0)
C
1

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.
Cilicia answered 19/11, 2009 at 9:24 Comment(0)
L
1

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.

Latoyalatoye answered 27/7, 2020 at 21:20 Comment(0)
E
1

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
Euphemiah answered 6/9, 2023 at 8:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.