9.4. Automating Tasks with Cron and Crontab files

Like most Linux users, you may find it necessary to schedule repetitive tasks to be run at a certain time. Such tasks can occur as frequently as once a minute, to as infrequently as once a year. This scheduling can be done by using the ``cron'' facilities.

The cron facilities as implemented in Linux are fairly similar to those available in other Unix implementations. However, Red Hat has adopted a slightly different way of scheduling tasks than is usually done in other distributions of Linux. Just as in other distributions, scheduling information is placed in the system ``crontab'' file (locating in the ``/etc/'' directory), using the following format:

minute hour day month year command

You can specify each time component as an integer number (eg. 1 through 12 for the months January through December), or specify one or more components as ``*'' characters which will be treated as wildcards (eg. acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html in the month component means the command will run at the given day and time in every month. Here are some examples:

# Mail the system logs at 4:30pm every June 15th. 30 16 15 06 acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html for x in /var/log/*; do cat ${x} | mail postmaster; done # Inform the administrator, at midnight, of the changing seasons. 00 00 20 04 acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html echo 'Woohoo, spring is here!' 00 00 20 06 acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html echo 'Yeah, summer has arrived, time to hit the beach!' 00 00 20 10 acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html echo 'Fall has arrived. Get those jackets out. :-(' 00 00 20 12 acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html echo 'Time for 5 months of misery. ;-('

Note that commands which produce output to standard out (ie. a terminal) such as the examples above using ``echo'' will have their output mailed to the ``root'' account. If you want to avoid this, simply pipe the output to the null device as follows:

00 06 acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html acknowledgements.html additional-docs.html administrative-issues.html apache-config.html backup-and-restore.html booting-install.html changing-user-passwords.html checking-storage-space.html choosing-linux.html copyright-and-disclaimer.html crash-repair.html creating-user-accounts.html custom-config.html disabling-user-accounts.html domain-name-server.html enterprise-computing.html hardware-raid.html help-plea.html index.html install-config.html install-configuration.html install-diskette.html install-format.html install-lilo.html install-packages.html install-partitioning.html install-swap.html internet-user-authentication.html introduction.html keeping-up-to-date.html kernel-custom.html kernel-upgrade.html linux-2.2.x.html linuxconf.html linux-kernel-upgrades.html linux-myths.html linux-overview.html managing-processes.html netatalk-file-and-print.html nfs-services.html performance-tuning.html preface.html redhat-processes.html removing-user-accounts.html root-account.html router-configuration.html samba-file-and-print.html scope.html security.html sendmail-upgrades.html server-backup.html server-migration.html server-restore.html shadow-file-formats.html squid-upgrades.html system-shutdown-and-restart.html trouble-in-paradise.html unsupported-tips.html update-redhat.html upgrading-linux.html user-perspective.html using-cron.html using-gnome.html using-kde.html using-rpm.html using-tarballs.html various-and-sundry.html web-server-administration.html what-is-linux.html where-to-turn.html xwindows-configuration.html xwindows-fonts.html xwindows-winmgr.html xwindows-xconfigurator.html xwindows-xdm.html echo 'I bug the system administrator daily at 6:00am!' >/dev/null

In addition to the standard ``crontab'' entries, Red Hat adds several directories:

/etc/cron.hourly/ /etc/cron.daily/ /etc/cron.weekly/

As their names suggest, executable files can be placed in any of these directories, and will be executed on an hourly, daily, or weekly basis. This saves a bit of time when setting up frequent tasks; just place the executable script or program (or a symbolic link to one stores elsewhere) in the appropriate directory and forget about it.