Logrotate without root user access not working

The problem is that we don’t have EASY root access to servers. That’s why being able to run logrotate without being root is very handy. This is what I did after following articles on the internet.

  1. Create a directory /home/techyman/logrotate with two files logrotate.conf and logrotate.status
  2. Put your logrotate configuration files there in logrotate.conf
/home/techyman/glassfish4/glassfish/domains/*/logs/*.log {
        su techyman techyman
        daily
        copytruncate
        missingok
        rotate 30
        compress
        notifempty
        dateext dateformat -%Y-%m-%d-%s
}
  1. Run a cron job
0 1 * * *  /usr/sbin/logrotate -s /home/techyman/logrotate/logrotate.status /home/techyman/logrotate/logrotate.conf

This runs everyday at 1 AM.
To test every 1 second, just do * * * * * in cron job.
But it’s not working. When I force it, it says "bad line at top in status file", otherwise no actions are being seen. I just see application.log alone.

How do I fix this issue? What am I doing wrong?

I removed notifempty and still it’s not working.

I then removed su techyman techyman and still it’s not working.

Finally manually doing this works. But the cron job still doesn’t work.

/usr/sbin/logrotate -s /home/techyman/logrotate/logrotate.status  
 -f /home/techyman/logrotate/logrotate.conf

Output of head -5 logrotate.status

logrotate state -- version 2
"/home/techyman/glassfish4/glassfish/domains/b/logs/application.log" 2023-11-19-17:46:26
"/home/techyman/glassfish4/glassfish/domains/domain1/logs/application.log" 2023-11-19-17:46:26
"/home/techyman/glassfish4/glassfish/domains/e/logs/application.log" 2023-11-19-17:46:26
"/home/techyman/glassfish4/glassfish/domains/c/logs/application.log" 2023-11-19-17:46:26

I removed daily from logrotate.conf hoping it would fix the issue but it still doesn’t work from cron.

Asked By: barnyard9

||

The message bad top line in state file indicates that the state file (the one you specified with the -s option) does exist, but its first line is not either

logrotate state -- version 1

or

logrotate state -- version 2

If you created the file yourself (e.g. with touch /home/techyman/logrotate/logrotate.status), then this error is expected. If the file does not exist, logrotate will automatically create it on the first run if the directory permissions allow it, and will add the proper header line automatically. After running logrotate once with the -f option, it may have fixed it automatically.

Because logrotate updates the state file by first creating a new file and then renaming it to the old state file, the directory permissions must allow logrotate to create files to the state file directory, or else updating the state file won’t work at all.

Also note this on logrotate(8) man page:

Normally, logrotate is run as a daily cron job. It will not modify a
log more than once in one day
unless the criterion for that log is based on the log’s size and logrotate is being run more than once each day, or unless the -f or –force option is used.

In your logrotate.conf, the option daily specifies that the logs are to be rotated once per day only. If you change it to hourly, you can get the logs rotated once per hour, but running logrotate more than once per hour will still be useless.

If you want the logs rotated more ofthen than once per hour, you must remove all time interval options and specify a size option instead.

Also, running logrotate every minute, especially with the compress option enabled, makes it likely that the previous logrotate job has not yet finished when the next one is trying to start. logrotate attempts to lock the state file when it starts, in order to prevent this from happening.

Answered By: telcoM
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.