In Linux there is a feature “logrotate” and cronjob. These two features are designed for different purpose but when you use them both, you can automate managing your server efficiently.
To see if the logrotate function is working, you can use this command.
systemctl list-timers | grep logrotate
Thu 2025-11-06 00:00:00 KST 9h left Wed 2025-11-05 00:00:47 KST 14h ago logrotate.timer logrotate.service
You can get the result of the scheduled task.
Before taking a look at the logrotate, Let’s take a look at the crontab. you can see the list of crontab tasks by using this command.
# To see the available tasks
crontab -l
# To modify crontab tasks
crontab -e
The basic structure of the crontab task looks like this.
* * * * * <command>
each * has their own jobs
*(first one) : 0-59(range/min)
*(2nd) : 0-23(range/hour)
*(3rd) : 1-31(range/days)
*(4th) : 1-12(range/month)
*(5th) : 0-7 (range/sun-sun)
So If you want to run a task every 10 min on monday, you need to set your crontab like this.
*/10 * * * 1 <<command>>
If you want to run a shell script with cronjob,, you can assign the file like this. It means run the scripts.sh at min 10 of every hour.
10 * * * 1 scripts.sh
Okay then, How should we combine these two features. Before explanation, let’s take a look at the log rotate. What’s logrotate. Let’s take a look at the configuration file first.
vi /etc/logrotate.conf
# see "man logrotate" for details
# global options do not affect preceding include directives
# rotate log files weekly
weekly
# use the adm group by default, since this is the owning group
# of /var/log/syslog.
su root adm
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
#dateext
# uncomment this if you want your log files compressed
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d
# system-specific logs may also be configured here.
logrotate is the feature that split the size of the log file under the path /var/log. /var/log directory is the place where important logs are stored. you can decide the duration of the backup
<to be continued>
댓글 남기기