How to Create a auto scheduling task Using PHP (Cron Job or Scheduled Task)
Basically, we will start by creating a schedule. Then, we will feed this schedule to a program called crontab. Crontab will take the schedule and install it into an internal set of tables which it manages. At the appropriate time demanded by your schedule, another program called cron will execute the tasks you have set.
Cron jobs, created using the command line program called crontab, require that your website be hosted on a Unix-type web server, such as Linux or one of the BSDs. Generally, if the web server that your site is on is running Windows, you cannot use this tutorial. Note that I am referring to the computer which your web host uses to place your site, not your own computer.
You also need to be able to connect to your web host using telnet or SSH. Alternatively, your web host must provide you a way to set the crontab tasks using their control panel.
This tutorial assumes that you have shell access, which means that you can connect to your web server using SSH or telnet and work from the command line on that computer. However, even if you can only set cronjobs from your web host’s control panel, once you learn the basics of the crontab commands, you should be able to easily transfer that knowledge to using the control panel. As such, this tutorial should still be useful to you, since even the most user-friendly control panel I’ve encountered is usually quite cryptic about how you can set up a cron job.
Steps to Setting Up a Cronjob on Your Web Server
Figure out how to write the schedule for crontab:
- The basic format of a crontab schedule consists of 6 fields, separated by spaces, formatted as follows:
minute hour day month day-of-week command-line-to-execute
The acceptable values for each of the 6 fields are:
|Field||Range of values|
|day-of-week||0-7 (where both 0 and 7 mean Sun, 1 = Mon, 2 = Tue, etc)|
|command-line-to-execute||the command to run along with the parameters to that command if any|
The fields have to be in that exact order, with no empty or missing fields.
“Minute” is a number from 0 to 59. “Hour” is a number from 0 to 23. They represent the time of the day in a 24-hour day format, so for example, if you want a certain command to run at 5.30 am, you will have to code it as:
If you want something run at 8 pm, it has to be coded as
since 2000 hours is 8 pm in the 24-hour time format.
“Day” and “month” refer to dates. “Day” takes a value between 1 and 31, and “month”, as you may have already guessed, can take any value between 1 and 12. So if you want a command run on 5th January at 9.15 am, your schedule should begin with the following:
15 9 5 1
“Day-of-week” means basically which day you want your command to run. If you want your command to run on Sundays, use either 0 or 7 here. If you want it on Monday, use 1. (Note: if you are getting worried at this point how to combine all the various fields, some of which seem to contradict the other, don’t worry. We’re getting to that.)
The trick to scheduling things, say, once a day, or once in 2 hours or the like, is to use a wildcard character. A wildcard character is like the Joker in a pack of playing cards. It can represent any card in the pack. Similarly, in a crontab file, the wildcard character “*” (the asterisk, without the quotes), represents every possible value for the field.
If you want a particular program to run, say, once every day at 10.45 am, the time portion of the cron schedule should read:
45 10 * * *
Here’s how to read the above line.
The first two fields “45 10” means that you want it to run at 10.45. The next field, the day field, is set to * (the asterisk character) to show that we’re talking about 10.45 every day, not just the 1st of the month (which would be “1”) or the 30th of the month (“30”) or some other number.
The month field is set to the asterisk as well. If we set some number in the month field, say “2”, we will be saying that we only want the command to run at 10.45 in the month of February (“2”). Since that’s not what we need, we put the asterisk to mean every month.
Similarly, the day-of-week field is set to the asterisk, because we want the command to run whether it’s Sunday (“0”) or Monday (“1”) or whatever day.
More Examples: An Hourly Schedule
Now if you want a job to run every hour on the hour, you will have to set the time component of the crontab line as follows:
0 * * * *
Can you see why? The “0” means at the top of the hour, that is, when the minute readout on a digital clock shows “00”. The asterisk in the hour field means every single hour. In other words, every hour, on the hour.
Alternate Hour or 3 Hourly Schedule
If you want something to run once every two hours, you will have to use the slash, “/”, character in your field. The slash character is the “step” character. In the case of a two hourly schedule, your time component of your cron file will read:
0 */2 * * *
The second field, “*/2”, means every alternate hour.
Similarly, if you want something to run every 3 hours, you can change that field to “*/3”, and so on.
If you want a particular command to run only at 8.00am on the 1st and 20th of every month, you should code the time as:
0 8 1,20 * *
The comma, “,”, means “and”. If you are confused by the above line, remember that spaces are the field separators, not commas.
What does the following schedule mean?
2 3 4,5 6 7
Decoded, the above line says at 3:02 am on the 4th and 5th of June (6) and on every Sunday (7), run your program.
There are other possibilities in the time fields, and I won’t go through all of them, since you already know enough to be able to construct whatever schedule you need.
· How to specify the command line
The command-line-to-execute portion of the schedule is basically the command you want run at the specified time. For example, if you have a Perl script called “whatever.pl” that you want run every day at 11.30 pm, your crontab schedule might read as follows:
30 11 * * * /your/directory/whatever.pl
If your script is one of those that must be called from a web browser, like “cron.php” on a Drupal installation, you will need to use a command called “wget”. Technically, wget is not really a browser, but it works adequately like one for our purpose here, which is to simply get the web server to run the script called “cron.php”.
30 11 * * * /usr/bin/wget http://www.example.com/cron.php
In this case, you will need to specify the full path to wget since, for security reasons, the cron program (not the cron.php script) usually executes in a sort of clean environment, which means that it won’t know how to find the command “wget” unless you tell it where it is.
Another important thing to note about this crontab line that we’re constructing is that the entire line, schedule and command to execute, must fit into one line. You cannot put it into two lines for aesthetic reasons even if your command is very long.
Upload the file to your web server using an FTP program
Now upload the file to your site using your FTP program. The file must be uploaded in ASCII mode, which your FTP program will probably automatically do when it sees that you’re uploading a file with a “.txt” extension.
· Connect to your web server’s shell account
Connect to your web host’s shell using your telnet or SSH software. If you don’t have one, download one from the Free SSH (Secure Shell) and Telnet Clients page. I will not go into the details of how you should use the software to log into your shell account, since the method varies according to the program you have downloaded. Ask your web host if you have problems.