22

How best can logrotate be configured, on a per-user basis, to rotate files in the home directory of the user, under control of a per-user crontab -e?

A.B.
  • 89,123
  • 21
  • 245
  • 323
Bryce
  • 1,857
  • 2
  • 18
  • 30

1 Answers1

33

Try this procedure:

  1. create /home/user/logrotate folder

    mkdir /home/user/logrotate
    
  2. create /home/user/logrotate/my.conf configuration file with logrotate directive as you need

  3. create /home/user/logrotate/cronjob to run logrotate every day at 2:30 AM (this is an example)

    30 2 * * * /usr/sbin/logrotate -s /home/user/logrotate/status /home/user/logrotate/my.conf > /dev/null 2>&1
    
  4. check your configuration file syntax:

    logrotate -d /home/user/logrotate/my.conf
    
  5. configure crontab to run logrotate (Warning: This removes existing entries in your crontab. Use crontab -e to manually add the line from step 3 to an existing crontab):

    crontab /home/user/logrotate/cronjob 
    

After this last command, logrotate will rotate file as described in /home/user/logrotate/my.conf and save log file status in /home/user/logrotate/status.

Use:

crontab -r   # remove crontab activities for user
crontab -l   # to list crontab activity for user
crontab -e   # edit user crontab entries

Here is logrotate and crontab man page.

jurgispods
  • 203
  • 2
  • 4
Lety
  • 5,994
  • 2
  • 28
  • 36
  • 5
    Very instructive. Maybe it should be mentioned that 'crontab ' removes all previously configured cronjobs. This just happened to me - luckily I had a backup :) – jurgispods Feb 20 '16 at 15:23
  • 1
    @pederpansen thanks for having improved my answer :) – Lety Feb 22 '16 at 09:17
  • 2
    Thanks to the `-s` parameter will avoid `error: error creating unique temp file: Permission denied`. – Marco Marsala May 10 '16 at 08:18
  • 1
    Running as normal user I get `error: error creating output file /var/lib/logrotate/status.tmp: Permission denied` – realtebo Aug 27 '20 at 07:48
  • 2
    @realtebo parameter -s /home/user/logrotate/status should avoid this problem. Did you use it? – Lety Aug 28 '20 at 09:50
  • @Lety, thanks, `-s` param solves the problem, so I can run as normal user, specifi my config file to execute and also using `-s` I can specify state file. Thanks, really – realtebo Aug 28 '20 at 12:47