Pin Me

Linux Command Line: Cron/Crontab

written by: jlwallen•edited by: Christian Cawley•updated: 11/27/2008

With the Linux operating system, you can automate nearly any task with the help of crontab. Crontab maintains files that are used by the cron system which is at the heart of Linux automation. This article will show you how to master crontab with ease.

  • slide 1 of 4

    History

    The cron/crontab system came to life in version 7 of the AT&T UNIX. The only problem the original system had was that it demanded too much from the machine's resources because it could only be run as the root user. So, the root user was having to handle all cron jobs for every user on the system. The second iteration of cron was changed so that the root user handled root processes and each user could install their own cron jobs. This was immediately successful and became the cron we know today.

  • slide 2 of 4

    How Cron Works

    The cron system works by reading a crontab files found in various directories on the system. The following directories house root and system crontabs:

    /etc

    /etc/cron.d

    /etc/cron.daily

    /etc/cron.hourly

    /etc/cron.monthly

    /etc/cron.weekly

    As the root user you can directly edit the /etc/crontab file, but using the crontab system is a much better way to create new cron jobs.

    Once a cron job is created with the crontab system, that job will be executed at the precise time configured in the entry. Now this brings up an all-to-often confusing issue - time. The cron system looks at time like this:

    Minute(0-59)

    Hour (0-23)

    Day of the month (1-31)

    Month (1-12)

    Day of the week (0-6 with Sunday being 0)

    Where you need to configure a section of time, you would use the numerical value. If there was not a value to configure, an "*" would be put in its place. So if you needed to configure a cron job to be executed at 7:01 am on the th of October you would enter:

    01 7 30 10 *

    But that is only part of the entry. The next portion would be the command that needed to run. So, if you had a script you had written(we'll call it script and place it in home directory of the user jlwallen) at this time the entry would look like:

    01 7 30 10 * /home/jlwallen/script

  • slide 3 of 4

    Using crontab

    As stated earlier, the root user can always manually edit the /etc/crontab file. But, the better way to add cron jobs (and the only way a standard user has of adding jobs) is to use the crontab tool. To invoke crontab, you only have to issue the command crontab from a command line prompt. There are three arguments to choose from: -e, -l, -r. If you issue the command crontab -e, you will be placed in edit mode where you will edit your user crontab with your chosen editor. (This will vary with what is on your system. I recommend Alpine.) The crontab -l command will list your current crontab entries. The crontab -r will remove all crontab entries from the user issuing the command.

    So, if using the Alpine editor and you want to enter a cron job that will execute the /home/jlwallen/script command at 7:01 am Oct 30 you would issue the command crontab -e and then enter the following text:

    01 7 30 10 * /home/jlwallen/script

    Once the text is entered hit the Control key and the X key together to save the crontab file. You could then enter crontab -l which will return a listing of the crontab entries like so:

    # m h dom mon dow command

    01 7 30 10 * /home/jlwallen/script

  • slide 4 of 4

    Final Thoughts

    Crontab is an incredibly useful tool to automate tasks in Linux. Once you get beyond understanding how cron sees time listings, you will have no problems creating crontab entries to make your administration life easier.