Logs – How Does Logrotate Work?

logrotatelogs

I'm really struggling to understand how logrotate works when running a command within a shell file of my own, and how it doesn't.

The command in question is:

rclone -L -vv --log-file "/home/mike/tmp/qqq.log"  sync "/media/mike/W10 D drive/My Documents/"  remote:MyDocuments_M17A_from_Linux

What I would like:

I would like this qqq.log file to be checked quite often, say every 10 min., to see whether it has exceeded a given size, say 1 MB. And if it has, to rotate the file. rclone with the -vv option produces copious output, deliberately (i.e. to try to understand to get logrotate working for this use case).

There's a moderately helpful tutorial here, but it still leaves me in the dark:

  1. How are you meant to configure rotation of logs from your own non-system processes? Are you meant to stick lines at the bottom of /etc/logrotate.conf? This is what I have done (under "#system-specific logs may be configured here"):

    /home/mike/tmp/qqq.log {
      notifempty
      size 1M
      daily
      create 0664 root root
      rotate 3
    }
    

    later: I now realise you can also put individual files in /etc/logrotate.d – no great complexity there.

  2. Is it the case that by default logrotate only runs once a day, by virtue of the file /etc/cron.daily/logrotate?

  3. Does that mean that if I want much more frequent checking I should perhaps set up a cron job to do this, running (in this example) every 10 minutes?

  4. Are you meant to run logrotate <config file> as root? I ask this question because I have tried both as root and as user. User didn't seem to work.

A link to a beginner's guide to this sort of setup, which can't be that uncommon, would be helpful. I have searched but most of what I've found doesn't give you a step-by-step guide.

Best Answer

logrotate can be run as an ordinary user, without administrative privileges, to rotate logs for that user.

# Create a local per-user configuration file
cat >.logrotate.conf <<'X'
/home/mike/tmp/qqq.log {
    notifempty
    missingok
    size 1M
    rotate 3
}
X

# Run logrotate with that configuration file
/usr/sbin/logrotate -v -s .logrotate.state .logrotate.conf

I've removed your daily criterion because you wanted purely a size-based check, and this would have limited any possible action to just once a day (the first time each day that logrotate is run, as it happens). I've replaced create with missingok so that it's up to your actual rclone job to create the output file rather than logrotate.

Then put the logrotate command into your user's crontab file:

# Capture any existing crontab entries
crontab -l >.crontab

# Append ours to the list
echo '0 * * * * /usr/sbin/logrotate -s .logrotate.state .logrotate.conf >>.crontab.log 2>&1' >>.crontab

# Reload crontab
crontab .crontab

Using this example, output from the command will be written to .crontab.log, and you'll probably want a logrotate entry to cycle or reset it monthly.

Related Question