Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are variables supported in logrotate configuration files?

Tags:

logrotate

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?

like image 375
kzyapkov Avatar asked Nov 19 '09 09:11

kzyapkov


2 Answers

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.

like image 96
Rotator of Logs Avatar answered Oct 17 '22 06:10

Rotator of Logs


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.

like image 20
Alexander Pogrebnyak Avatar answered Oct 17 '22 05:10

Alexander Pogrebnyak