How to rotate dnsmasq log files
Environment
- Red Hat Enterprise Linux 8
- Red Hat Enterprise Linux 9
Issue
- How to use logrotate to rotate dnsmasq log file.
Resolution
- When
log-facilityis set to a specific log file, sent a SIGUSR2 signal to dnsmasq process to rotate log file.
postrotate
/usr/bin/kill -USR2 `cat /var/run/dnsmasq.pid 2> /dev/null` 2> /dev/null || true
endscript
- When
log-facilityis set to a specific facility device, dnsmasq log will be send to syslog. So need to send a SIGHUP signal rsyslog process to rotate log file.
postrotate
/usr/bin/systemctl kill -s HUP rsyslog.service >/dev/null 2>&1 || true
endscript
Root Cause
- When dnsmasq receives SIGUSR2, it closes and reopens the log file.
--log-facility=<facility>
Set the facility to which dnsmasq will send syslog entries, this defaults to DAEMON, and to LOCAL0 when debug mode is in operation. If the facility given contains at least one '/' character, it is taken to be a filename, and dnsmasq logs to the given file, instead of syslog. If the facility is '-' then dnsmasq logs to stderr. (Errors whilst reading configuration will still go to syslog, but all output from a successful startup, and all output whilst running, will go exclusively to the file.) When logging to a file, dnsmasq will close and reopen the file when it receives SIGUSR2. This allows the log file to be rotated without stopping dnsmasq.
-x, --pid-file=<path>
Specify an alternate path for dnsmasq to record its process-id in. Normally /var/run/dnsmasq.pid.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.
Comments