0

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.

6
  • Please run head -5 /home/techyman/logrotate/logrotate.status. What is the output? (Please edit your question, don't write it as a comment.)
    – telcoM
    Nov 19, 2023 at 12:07
  • Eyeballing the question, there is a difference between the manual command at the end and the command in the crontab: -f is missing in the latter. It may be a typo, it may or may not be the problem, but you should fix it anyway.
    – NickD
    Nov 19, 2023 at 13:27
  • The thing is I'm running the cron with * * * * * that is every second, so every second (even without -f it should do the log rotation)
    – barnyard9
    Nov 19, 2023 at 14:10
  • @telcoM did it.
    – barnyard9
    Nov 19, 2023 at 14:12
  • Unless you have a non-standard cron, a cron job specified as * * * * * is every minute, not every second. (it's "each minute of each hour of each day of each month, and every weekday)
    – telcoM
    Nov 19, 2023 at 14:16

1 Answer 1

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .