3

I am using log rotate to rotate logs for a daemon I have running (A web service). The log rotate is as follows:

/var/log/deamon/error.log {
    daily
    rotate 20
    compress
    delaycompress
    missingok
    notifempty
    create 644 uwsgi uwsgi
    postrotate
            /etc/init.d/deamon stop
            /etc/init.d/deamon start
    endscript
}

The /etc/init.d/deamon stop works fine but it never starts backup. There is one of two issues: either I get a permission denied error (Probably not because I would get it for stop too). Or the startup portion is failing. I need to start the service as root because I need to set a UID and GID due to permission concerns. Even if I set the permissions to -rwxrwxrwx it gives me this error: unable to set gid to 1001 (Operation not permitted) My question is how do I set logrotate to run as a different user (i.e root) or allow rotate to set the gid.

Here is my /etc/init.d/deamon

#!/bin/bash
daemon=/venv/deamon_django18/bin/uwsgi
pid=/var/run/uwsgi/deamon.pid
args="--ini /etc/uwsgi/deamon.ini --pidfile $pid"

case "$1" in
start)
    echo "Starting uwsgi"
    start-stop-daemon -u uwsgi -g uwsgi -p $pid --start --exec $daemon -- $args
    ;;
stop)
    echo "Stopping script uwsgi"
    start-stop-daemon --signal INT -u uwsgi -g uwsgi  -p $pid --stop  $daemon -- $args
    ;;
reload)
    echo "Reloading conf"
    kill -HUP $(cat $pid)
    ;;
*)
    echo "Usage: /etc/init.d/uwsgi {start|stop|reload}"
    exit 1
;;
esac

exit 0
2
  • logrotate runs as root by default, so I don't think that's where your problem lies. What user has ID 1001? Does the log rotation work, just without the correct file permissions? Does your /etc/init.d/deamon script start up the service outside of logrotate? – mjturner Jul 24 '15 at 8:40
  • @mjturner. Thanks for the comment. The user ID 1001 is the user the runs the application (I think the actual name is django_app). And yes. But only if I am running as root or the user django_app. – rady Jul 24 '15 at 23:15
6

In order to use a different user with logrotate you can specify the "su" option:

/home/ubuntu/log/*.log {
  su ubuntu ubuntu
  rotate 5
  daily
  compress
  missingok
}
1
  • Not available for logrotate-3.7.8 – Srikanth Sharma Jul 24 '19 at 8:55
2

In case anyone has a similar problem, I fixed it. Turns out the issue was the daemon wasn't shutting down fast enough. When start is called in the logrotate file, the program is technically still running. I solved it by adding a sleep In the init.d file and re writing reload to call stop and then call start. The final solution looked like:

...
reload)
    /etc/init.d/daemon stop
    sleep 2
    /etc/init.d/daemon start
    ;;
*)
...

And the logrotate:

...
postrotate
    /etc/init.d/daemon reload
endscript
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.