Cron and crontab on Debian

Author: John M. Gabriele | back to index

  • Cron and crontab on Debian
  • ---

    Overview

    Cron is used in two ways on Debian.

    Either way you slice it, some commands that get run by cron produce output to stdout. If they do, that output is sent as an email to the owner of the crontab (or, if set, to the value of the MAILTO environment variable). Recall that, (A) most unix commands run quietly, not producing any output unless necessary, and (B) almost every unix box has a mail server running (typically Exim, on Debian), if only to deliver this sort of local mail.

    Note: Regarding emails from crontasks, you might find that, depending on the mail server configuration, root and system crontasks (which also run as root) may send emails to a user other than root. You may find that there isn't even a mailbox for root (see /var/mail). Check your exim config (or run dpkg-reconfigure exim4-config) for details.

    As the root user, if you want to set up periodic tasks, your first choice should be to drop executable scripts into /etc/cron.<daily|hourly|monthly|weekly> . You can disable them if you like by simply using chmod -x on them. If daily|hourly|monthly|weekly doesn't offer enough fine-grained control, use crontab -e. It's probably best not to edit /etc/crontab, but this is mostly personal preference. As a side note, be sure to include /var/spool/cron/crontabs in your list of directories to regularly back up. :)

    Using crontab

    As any user on the system, you may run the crontab command which accesses that users crontab file located in /var/spool/cron/crontabs. When you run crontab -l it shows you the current crontab for the user you're currently logged-in as. When you run crontab -e it opens an editor for you (usually vi) to modify your crontab.

    Cron files in /etc

    In /etc, you've got the following:

    Each of those cron.{daily,hourly,monthly,weekly} directories contain plain ole' shell scripts intended to run {daily,hourly,monthly,weekly}. Just drop a script into one of these directories, and it'll get run {daily,hourly,monthly,weekly} automatically. Pretty nice. :)

    In /etc/crontab, you'll see two commands being used: run-parts and anacron. They are responsible for running those scripts in {daily,hourly,monthly,weekly}.

    run-parts

    run-parts comes with the debianutils package. It's just a handy little program which simply runs all the scripts (one after another) in a directory whose name you pass as an argument.

    Anacron

    You may or may not have this installed. See dpkg -l | grep anacron to find out. By default you won't have it. See apt-cache show anacron for more details.

    By default, the {daily,monthly,weekly} tasks are set to run a little after 6 AM. If your computer is usually shut down at that time, unless you have anacron installed, I don't believe those cron tasks will get run.

    Links