Logrotate: Docker + nginx. Can't reload nginx container (logrotate: ALERT exited abnormally with [1])

1.8k Views Asked by At

I have a logrotate config:

/opt/docker_folders/logs/nginx/*.log {
    dateext
    daily
    rotate 31
    nocreate
    missingok
    notifempty
    nocompress
    postrotate
    /usr/bin/docker exec -it nginx-container-name nginx -s reopen > /dev/null 2>/dev/null        
    endscript
    su docker_nginx root
}

folder permissions:

drwxrwxr-x. 2 docker_nginx root   4096 Oct 13 10:22 nginx

nginx is a local host folder mounted to docker container.
docker_nginx is a user that has same uid as nginx user inside a container (uid: 101).

If I run commands (as root)

# /sbin/logrotate -v /etc/logrotate.d/nginx_logrotate_config
# /sbin/logrotate -d -v /etc/logrotate.d/nginx_logrotate_config
# /sbin/logrotate -d -f -v /etc/logrotate.d/nginx_logrotate_config

All working like a charm.

Problem: But when log rotating automatically by cron I have get error

logrotate: ALERT exited abnormally with [1]

in /var/log/messages

As result logs rotating as usual but nginx don't create new files (access.log, etc). Looks like postrotate nginx -s reopen script failing.

Linux version is CentOS 7. SELinux disabled.

Question: At least how know what happend when logrotate running from cron? And what problem may be?

PS I know that I can also use docker restart. But I don't want to do this because of service short time disconnect.

PS2 Also I know that here is nocreate parameter in config. That is because I want to create new log files by nginx (to avoid wrong permissions of new files). Anyway, if nginx -s reopen really failing, there is a possibility that nginx will not re-read newly created files.

EDIT1:
I edited /etc/cron.daily/logrotate script and get logs. There is only one line about problem.

error: error running non-shared postrotate script for /opt/docker_folders/logs/nginx/access.log of '/opt/docker_folders/logs/nginx/*.log '

So I still don't understand what cause this problem... When I run this script manually all running fine.

1

There are 1 best solutions below

0
On BEST ANSWER

Okay. Answering by myself.

-it parameters can't be used with cron tasks (and logrotate is also a cron task). Because cron don't has interactive session (TTY).

I figured it out by running the /usr/bin/docker exec -it nginx-container-name nginx -s reopen > /dev/null 2>/dev/null as a cron task. I have got error message "The input device is not a TTY"

So my new logrotate config looks like

/opt/docker_folders/logs/nginx/*.log {
    dateext
    daily
    rotate 31
    nocreate
    missingok
    notifempty
    nocompress
    postrotate
    /usr/bin/docker exec nginx-container-name /bin/sh -c '/usr/sbin/nginx -s reopen > /dev/null 2>/dev/null'       
    endscript
    su docker_nginx root
}

And it's finally works.

I have to understand the parameter before using it
I have to understand the parameter before using it
I have to understand the parameter before using it