1and1 Help Centre Categories

print article

What are Cron Jobs?

Cron Jobs, in simplest terms, are jobs set to execute at a specific time. These "jobs" are actually scripts written in PHP, Perl, BASH, etc. Any script that can be run from the command line, can be scheduled to run automatically at a specific time by creating a Cron Job.

Cron Jobs are created via the Shell (or command line), so only 1&1 packages with SSH access are able create Cron Jobs.

Example Scenario
John Smith has a motivational and self-help website. He decides that in addition to the articles he writes, he would also like to offer an audio file for download/streaming. He feels that if he could record himself reading the article and allow users to download/stream the audio file, he could really motivate people. On top of that, it would allow visitors to save the audio to mobile audio device or CD for a listen later when they do not have access to his website.

He records the audio, uploads it and waits. All of a sudden he worries that with the building traffic he has been receiving, perhaps the 1&1 server he is using may not be able to accommodate many users downloading a large audio file. John is a wiz at programming so he quickly writes a BASH script to check his web server logs for failed file transfers to gather more info about his suspicion. He saves the script as failedXferCheck.sh. He runs the failedXferCheck.sh script he just made from the command line to find out that a few file transfers have failed. This worries him, so he decides to create a Cron Job that will run this script at 7 am the next morning and 7 am every morning after to keep an eye on things for a while. He alters the script slightly so that he is e-mailed the results. Since he checks his E-mail every morning while drinking his coffee, he will be able to quickly check the results before getting started on work for that day.

Technical Detail
Once the creation of the failedXferCheck.sh script was completed, scheduling the script to be run requires some additional information. First we will look at the Cron Job entry John Smith used for his script for a basic overview. Other articles can be referenced later for further detail or basic "How-to" instructions.
John Smith's Cron Job Entry
0 7 * * * /usr/bin_uk/bash /homepages/11/d123456789/htdocs/motivationSite/cron/failedXferCheck.sh

This may seem extremely technical at first, but really there are only three parts to a Cron Job entry.
1st Part 2nd Part 3rd Part
Schedule Executable Script
0 7 * * * /usr/bin_uk/bash /homepages/11/d123456789/htdocs/motivationSite/cron/failedXferCheck.sh

The Schedule specifies when or how often the script should be run. This can be any number of options from once every hour, on the hour to 8:35 am on Sundays to 15 minutes after the hour, every other hour, on the 21st-28th of each month. As you can see, the scheduling can become quite complex.

The schedule format is as below:

Schedule Field Minute Hour Day of Month Month Day of Week
Example 0 7 * * *
Explanation 0 minutes past the hour of the 7th hour (24hr time) every day of every month every day of the week (Sun-Sat)

An asterisk(*) signifies every option. Using the example above you will notice that the script John Smith is running is set to execute at 7 am every single day.

The Executable is the application that should run the process. This is not actually required by the Cron Job as the system should automatically run the script with the correct application according to its extension (so that .php file are run using PHP and .sh files are run using BASH) however it is best to specify.

The script is the file that contains the code to be executed. This code can be PHP, Perl, Python, BASH, etc. Using the full folder path to the file is necessary. Using only the filename without the folder path will likely result in errors.

Using this information, you should now have a general understanding of Cron Jobs.

For additional information, you may want to reference: