Daily logRotate for apache at specific time

9,339

Step #1 - create script

You can create a file such as this:

$ sudo gedit /etc/cron.d/logrotate

And add these lines to this file:

#!/bin/bash

/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
    /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

Step #2 - add script to crontab file

Then create a crontab entry that runs this script at 3h30 each day. To do this 2nd step edit the file /etc/crontab:

$ sudo gedit /etc/crontab

And add this line:

# m h dom mon dow user  command
30 3 * * *  root    /etc/cron.d/logrotate

NOTE: You might need to omit the user in some situations, like this:

# m h dom mon dow   command
30 3 * * *      /etc/cron.d/logrotate

Step #3 - make script executable

Lastly make the logrotate shell script (/etc/cron.d/logrotate) executable:

$ sudo chmod +x /etc/cron.d/logrotate

References

Share:
9,339

Related videos on Youtube

Abdelilah Benaou
Author by

Abdelilah Benaou

System and network administrator with a history of working in the computer networking industry. My play field includes FreeBSD/Linux, bare metal hyper-visors (mainly ESXI), Amazon AWS, networking hardware/software and to a lesser extent Microsoft Products. graduated from the university of Picardie Jules Verne with a bachelor’s degree in computer science (Amiens).

Updated on September 18, 2022

Comments

  • Abdelilah Benaou
    Abdelilah Benaou almost 2 years

    How can I execute logRotate daily at a specific time (3h30) each day? Specific details on how to do this would be appreciated.

    I'm on Debian.

  • Abdelilah Benaou
    Abdelilah Benaou over 10 years
    Thanks a lot, I have set up the scripts and I am waiting to see if it works, hope so.
  • Abdelilah Benaou
    Abdelilah Benaou over 10 years
    I had to remove the root login in order to make it work.
  • slm
    slm over 10 years
    @AbdelilahBenaou - did you change it to something else or completely delete it? LMK so I can update the A.
  • Abdelilah Benaou
    Abdelilah Benaou over 10 years
    I changed the cron job line which goes like this : Before : 30 3 * * * root /etc/cron.d/logrotate After : 30 3 * * * /etc/cron.d/logrotate
  • slm
    slm over 10 years
    @AbdelilahBenaou - thanks I've added an example like this and a note.