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
?
1 Answer
Try this procedure:
create
/home/user/logrotate
foldermkdir /home/user/logrotate
create
/home/user/logrotate/my.conf
configuration file with logrotate directive as you needcreate
/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
check your configuration file syntax:
logrotate -d /home/user/logrotate/my.conf
configure
crontab
to runlogrotate
(Warning: This removes existing entries in your crontab. Usecrontab -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
-
5Very instructive. Maybe it should be mentioned that 'crontab <file>' removes all previously configured cronjobs. This just happened to me - luckily I had a backup :) Feb 20, 2016 at 15:23
-
1
-
2Thanks to the
-s
parameter will avoiderror: error creating unique temp file: Permission denied
. May 10, 2016 at 8:18 -
1Running as normal user I get
error: error creating output file /var/lib/logrotate/status.tmp: Permission denied
– realteboAug 27, 2020 at 7:48 -
2@realtebo parameter -s /home/user/logrotate/status should avoid this problem. Did you use it?– LetyAug 28, 2020 at 9:50