How does logrotate work?

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.


As you are producing your own log files and require a more real-time size chopping, you should take a look at the Apache utility rotatelogs here.

It works by reading stdin, and chops up the logfile based on command line arguments.

eg.

program-writing-to-stdout|/bin/rotatelogs /home/mike/tmp/qqq.log 1M"

logrotate on the other hand, checks the logfiles when it is run, and usually systems are setup to run logrotate (via cron) once per day. The configuration of sub-systems is usually done by dropping the configuration file into /etc/logrotate.d and yes, it usually runs as root.

Edit: @mike rodent

logrotate is run (by cron or by hand) and then checks the logfile statistics as seen in the file-system, it then triggers a rotation depending on it's configuration.

rotatelogs reads it's standard input continuously appending to a logfile, then when the time or the size of logfile reaches a trigger-point, it closes the current logfile, creates a new logfile (appropriately named), then continues to append to the new logfile.

This has a bearing on how you start the program which generates the output, it needs to write to standard output and then you pipe this into rotatelogs.

eg

while true ; do date ; sleep 30 ; done | rotatelogs -n 10 /home/mike/tmp/qqq.log 60

Will give you a circular rotation through 10 files, cut every 60 seconds

Using rclone, I would expect --log-file "/dev/stdout" would make it write to standard output.

[edit 2] @mike rodent

I'm not familiar with rclone, whether it writes to stdout or stderr and what if anything it writes to either, but there are ways around this, even if the --log-file=/dev/stdout didn't work.

This StackOverflow Q/Answer, amongst others explains redirection and piping. But to summarise, it's possible to redirect stderr only or merged with stdout into a pipe.

Bash example:

Merge stderr with stdout into pipe

FirstCommand 2>&1 | OtherCommand

Only stderr into pipe, stdout to otherfile (which could be /dev/null or even just a hyphen - which closes stdout).

FirstCommand 2>&1 1>otherfile.log | otherCommand

These is examples use the older bash syntax, bash v4 has a modern less verbose variant.

Tags:

Logrotate

Logs