Subject: /etc/cron.d vs. /etc/cron.daily
Author: Linux
In response to: What's file /etc/crontab then? What's difference between /etc/crontab and /var/spool/cron/crontabs ?
Posted on: 09/07/2012 06:35:17 PM
What's difference between those two directories /etc/cron.d and /etc/cron.daily ?
As shown above in system crontab file /etc/crontab, all scripts within directory /etc/cron.daily will be executed daily (50 1 * * *) by system root's environment . For example, the script logrotate which rotates log file is a very good example to be here, as it needs to be run daily to ensure the disk capacity.
$ cat /etc/cron.daily/logrotate
#!/bin/sh
/usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0
/etc/cron.d, however, contains scripts of package level. In addition to read
/etc/crontab,
cron reads as well the files in /etc/cron.d: it treats the files in
/etc/cron.d as in the same way as the
/etc/crontab file (they follow the special format of that file, i.e. they include the user field). However, they are independent of
/etc/crontab: they do not, for example, inherit environment variable settings from it. The intended purpose of this feature is to allow packages that require finer control of their scheduling than the
/etc/cron.{daily,weekly,monthly} directories to add a crontab file to /etc/cron.d.
So, two key points:
1) Varies in control;
2) Varies in environment settings.
For example, the script
awstats which update web access statistics based on access_log is a very good example to be here, as it needs to be run on package level (per vHost) and on finer control (you may want to run it at different time frame for each vHost).
List of scripts:
$ ls -l /etc/cron.d
-rw-r--r-- 1 root root 47 Jul 7 2008 awstatcronsetup
-rw-r--r-- 1 root root 151 Sep 6 23:55 awstats
AWStats cron setup:
$ cat /etc/cron.d/awstatcronsetup
55 23 * * * root /usr/share/awstats/cronsetup
AWStats script setup:
$ cat /usr/share/awstats/cronsetup
#!/bin/bash
hour=`echo $[$RANDOM % 24]`
if [ $hour -gt 9 ]; then
echo "00 $hour * * * root /usr/share/awstats/awstats_updateall.pl now
-awstatsprog=/var/www/cgi-bin/awstats/awstats.pl
-configdir=/etc/awstats/ >/dev/null 2>&1" >/etc/cron.d/awstats
else
echo "00 0$hour * * * root /usr/share/awstats/awstats_updateall.pl now
-awstatsprog=/var/www/cgi-bin/awstats/awstats.pl
-configdir=/etc/awstats/ >/dev/null 2>&1" >/etc/cron.d/awstats
fi
/sbin/service crond restart >/dev/null 2>&1
AWStats update cron script:
$ cat /etc/cron.d/awstats
00 18 * * * root /usr/share/awstats/awstats_updateall.pl now
-awstatsprog=/var/www/cgi-bin/awstats/awstats.pl
-configdir=/etc/awstats/ >/dev/null 2>&1
>
> On 09/07/2012 05:43:32 PM
Linux wrote:
There are both crontab files with different format. They are both loaded when system is on but run in different environments:
/var/spool/cron/crontabs/ --> user defined and run in user's environment
/etc/crontab --> system's environment
Here is the default content of /etc/crontab
$ sudo cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
36 * * * * root run-parts /etc/cron.hourly
50 1 * * * root run-parts /etc/cron.daily
0 4 * * 0 root run-parts /etc/cron.weekly
30 3 14 * * root run-parts /etc/cron.monthly
References: