1and1 Help Centre Categories

print article

Create a Cron Job

For Linux packages with SSH

Learn how to create a simple script and a Cron Job to schedule the execution of a script.

Step 2
Once logged in, type the following command to open the Nano text editor and create the new file crontTest.php: Type 'nano cronTest.php' and create a new cronTest.php file
Type 'nano cronTest.php' and create a new cronTest.php file

We will now create a PHP script to later schedule this via a Cron Job.
Step 3
In our PHP script, we will simply be using the mail function to send a mail of the current version of PHP to a specified address. You can simply copy the code below and paste it into the editor by clicking the right mouse button. You will need to replace the email address (emailaccount@domain.com) with your own:
<?php mail('emailaccount@domain.com','Cron Job Test Script',phpversion()); ?>

The script above is written in PHP and uses the PHP mail function. The first value in quotes is the mail address being sent to. Please replace this with your e-mail address. The second value in quotes is the Subject of the e-mail being sent. The third value not in quotes is the body of the email which in this case, will send the PHP version as the body of the e-mail. Input your information into the script
Input your information into the script
When finished, hold the Ctrl button and press the x key to exit the Nano editor. When asked to save changes, type the y key for yes and press ENTER. Press Ctrl+x on your keyboard, type y, and press Enter
Press Ctrl+x on your keyboard, type y, and press Enter
The filename should already be named cronTest.php. If there is a mistake in spelling, please make the appropriate changes and then press ENTER. Press Enter
Press Enter
Step 4
Next, type php cronTest.php at the command line and press ENTER to run the PHP script:Type 'php cronTest.php' and press Enter
Type 'php cronTest.php' and press Enter
Step 5
The PHP script should run and send an e-mail to the address used in the script. Check your SPAM and/or junk folder for the e-mail. If you do not receive the mail after a few minutes, go back to step two(2) to open the file in Nano and check the script against the example provided. Only the e-mail address should be different.
If you received the mail successfully, you may now proceed to the next step to use this script in a Cron Job.
Step 6
Go back to the command line. Type whereis php to find the location of the PHP binary file. This is the second piece of the Cron Job that we need. The path to the binary file is typically /usr/bin/php. Take note of this location. Type 'whereis php' and take note of the location
Type 'whereis php' and take note of the location
Step 7
Now that we have the PHP binary (or executable) path, we now need to know the full path to our cronTest.php file that has just been created. This is the third and last piece of the Cron Job that we need. Enter the following command to print working directory. This command will output the full path to the current folder you are in. Take note of this location. Type 'pwd' and take note of the location
Type 'pwd' and take note of the location
Step 8
Now type crontab -l and press ENTER afterwards to list any Cron Job entries currently set up in the Crontab. In this example, no Cron Jobs have been set up yet. Type 'crontab -l' and press Enter
Type 'crontab -l' and press Enter
Step 9
Enter the following command to edit the Crontab and to now add a new Cron Job. Then press ENTER: Type 'crontab -e' and press Enter
Type 'crontab -e' and press Enter
Step 10
The Crontab opens using the VI text editor. There may be one line already in the Crontab that starts with the hash symbol (#). All lines starting with the hash symbol are commented lines and will not be interpreted by Cron. Use your arrow keys to place the cursor at the end of the first line and press the i key to insert text at the current cursor position. If at anytime you need to exit the Crontab, simply press the Esc key and then type :q! for quit without(!) saving and press ENTER.
After pressing the i key to insert text at the end of the first line, press the ENTER key to start a new line. This new line will begin the first part of the Cron Job entry - the Schedule. To keep things simple, we will use the asterisk for each of the 5 fields with a space in between each. The entry should look like the image below so far. Type 'i' and press Enter, then type an asterisk into each field
Type 'i' and press Enter, then type an asterisk into each field
Schedule Field Minute Hour Day of Month Month Day of Week
Example * * * * *
Explanation every minute past the hour of every hour (24hr time) every day of every month every day of the week (Sun-Sat)

So referencing the table above, using asterisks(*) for every Schedule field signifies that the Cron Job should run every minute of every hour of every day of every month. This is not recommended because since our script sends an e-mail, one will be sent every minute. It is normally recommended to run Cron Jobs no frequently than every five(5) minutes so that jobs do not get queued however since we are only testing, running every minute will help us verify that the Cron Job is executing properly.
Step 11
Next, we will specify the executable. This is not required if the Cron Job is running a simple command (code that is not saved within a file) or if a file with an extension (e.g. some_file.php) is supplied however, it is recommended. Earlier in the article we ran the whereis command to find the location of the PHP binary (executable) file. Leave a space after the Schedule and type out this location. Enter the location of the PHP binary file
Enter the location of the PHP binary file
Step 12
Next, we will need to write out the command to run or the full path to the file containing the code. Since in this example, our code is contained in a file, we will need to specify the full path.
Earlier in this article we used the pwd command to show us the full path to the location where we created the cronTest.php file. Leaving a space after the Executable path, type the full path to the file. Type the full path to the cronTest.php file
Type the full path to the cronTest.php file

Next, don't forget to specify the filename at the end and precede it with a forward slash if necessary (in this case, add /cronTest.php) Specify the file name, preceded by a forward-slash
Specify the file name, preceded by a forward-slash
Step 13
Now, all pieces of the Cron Job are entered and to save the file and exit, press the Esc key and then type :x to exit and save, and press ENTER. Press the Esc key, type ':x' and press Enter
Press the Esc key, type ':x' and press Enter
Step 14
The Crontab should be updated. The Crontab is updated
The Crontab is updated

Check your E-mail. Within a minute or so, you should start receiving e-mails.
Step 15
Once you have received at least one E-mail from the Cron Job script to confirm it works, we will now have to disable the Cron Job so that you do not keep receiving the e-mails.
Go back to the command line and type the following command to edit the Crontab: Type 'crontab -e'
Type 'crontab -e'

Once the Crontab is open, use your arrow keys to place the cursor at the beginning of the newly added Cron Job line and press the i key to insert text. Then simply add the hash symbol(#) to the beginning of the line to comment out that Cron Job. This will keep Cron from reading that line and essentially stop that Cron Job. Press the i key and add '#' to the Cron Job line
Press the i key and add '#' to the Cron Job line

Press the Esc key and type :x and press ENTER to exit and save changes. Press Esc, type ':x' and press Enter
Press Esc, type ':x' and press Enter
The changes are saved
The changes are saved
Step 16
Keep an eye on your E-mail to ensure that you are no longer receiving mailing from the Cron Job script.

Quick Reference Examples

Schedule to run every minute of every day

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

Schedule to run every 15 minutes of every day

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

Schedule to run every day at 11PM

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

Schedule to run on the hour, every day from 7:00-16:00

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

Schedule to run every other hour from 9:00-21:00(9:00, 11:00, 13:00, 15:00, 17:00, 19:00, 21:00) on Sundays

Schedule Field Minute Hour Day of Month Month Day of Week
Example 0 9-21/2 * * 0
Explanation 0 minutes past the hour of every 2nd hour between the 9th-21st hours (24hr time) every day of every month that is a Sunday

Schedule to run every other hour from 9:00-21:00(9:00, 11:00, 13:00, 15:00, 17:00, 19:00, 21:00) on Sundays

Schedule Field Minute Hour Day of Month Month Day of Week
Example 0 9,11,13,15,17,19,21 * * 0
Explanation 0 minutes past the hour of every 9th, 11th, 13th, 15th, 17th, 19th, and 21st hour (24hr time) every day of every month that is a Sunday

*Same as above in a different format

Schedule to run every weekday at midnight

Schedule Field Minute Hour Day of Month Month Day of Week
Example 0 0 * * 1-5
Explanation 0 minute past the hour of the midnight hour (24hr time) every day of every month that is a Monday-Friday

Schedule to run at 9:03, 9:26, 9:40-9:50, 14:03, 14:26, 14:40-14:50 every 7th, 14th, 21st and 28th of October-February

Schedule Field Minute Hour Day of Month Month Day of Week
Example 3,26,40-50 9,14 */7 1,2,10-12 *
Explanation 3,26 and 40-50 minutes past the hour of the 9th and 14th hour (24hr time) every 7 days of the 1st, 2nd and 10-12th month every day of the week (Sun-Sat)
For additional information, you may want to reference: