Daily logRotate for apache at specific time
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
Related videos on Youtube
![Abdelilah Benaou](https://i.stack.imgur.com/LRRZ8.jpg?s=256&g=1)
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, 2022Comments
-
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 over 10 yearsThanks a lot, I have set up the scripts and I am waiting to see if it works, hope so.
-
Abdelilah Benaou over 10 yearsI had to remove the root login in order to make it work.
-
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 over 10 yearsI 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 over 10 years@AbdelilahBenaou - thanks I've added an example like this and a note.