Run logrotate as specific user

logrotatelogs

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

Best Answer

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
}
Related Question