Getting & Giving Support
- Ref. - PHPDoc
Wolf CMS books section
Available from Wolf CMS release: 0.6.0 (optional)
Cron is a daemon that executes commands at specified intervals. These commands are called “cron jobs.” Cron is available on Unix, Linux and Mac servers. Windows servers use a Scheduled Task to execute commands.
Wolf CMS itself does not require cron, but cron.php provides a cron_run event that can be used by plugins.
In the following example, the crontab command shown below will activate the cron tasks automatically on the hour:
0 * * * * wget -O - -q -t 1 http://www.example.com/wolf/app/cron.php
The above sample assumes you've installed Wolf CMS to the root of your site.
In the above sample, the 0 * * * * represents when the task should happen. The first figure represents minutes – in this case, on the “zero” minute, or top of the hour. (If the number were, say, 10, then the action would take place at 10 minutes past the hour.) The other figures represent, respectively, hour, day, month and day of the week. A * is a wildcard, meaning “every time.”
The rest of the line basically tells the server to “ping” the url http://www.example.com/wolf/app/cron.php.
Here is a diagram of the general crontab syntax, for illustration:
# +---------------- minute (0 - 59) # | +------------- hour (0 - 23) # | | +---------- day of month (1 - 31) # | | | +------- month (1 - 12) # | | | | +---- day of week (0 - 7) (Sunday=0 or 7) # | | | | | * * * * * command to be executed
Thus, the cron command example above means “ping http://www.example.com/wolf/app/cron.php at the zero minute on every hour of every day of every month of every day of the week.”
Cron jobs are scheduled by setting up a “crontab.” A crontab is a text file that contains the commands to be run. This file can be created and edited either through the command line interface, or, if you manage your website through a web-based control panel such as cpanel or Plesk, you will use the web interface. Check with your hosting company for detailed instructions if you are using a web-based control panel.
To edit a crontab through the command line, type:
If this fails, see the Troubleshooting Cron Jobs section below.
Add ONE of the following lines:
45 * * * * /usr/bin/lynx -source http://example.com/wolf/app/cron.php 45 * * * * /usr/bin/wget -O - -q -t 1 http://www.example.com/wolf/app/cron.php 45 * * * * curl --silent --compressed http://example.com/wolf/app/cron.php
Three options are provided in case either wget, lynx or curl are not installed on the server. Any will do the job well.
Learn more about the crontab file syntax here to set up the cron job to run more or less often.
There are many ways to configure a cron job. If you have full access to crontab features, you should be able to simply paste in one of the above example commands – be sure to replace “example.com” with your own web domain or docroot.
If you're on shared hosting, you should be able to find cron job configuration somewhere in your hosting control panel. Some hosts even have cron “wizards” that walk you through the cron configuration, making it much easier if cron is new to you. On a Windows system you can accomplish the same thing with scheduled tasks to launch Internet Explorer pointed to the URL.
If your hosting company restricts access to cron you have many options.
- Ask the company to give you access, or to set up a cron job for you
- Ask someone else with access to a server to set up a cron job for you. Any Unix, Linux, or Mac server with access to the internet can have a cron job to regularly visit your site. There are also some companies that offer cron services.
- Use the Poor Man's Cron option.
Cron doesn't guarantee your commands will run at the specified interval. But Wolf CMS will try its best to come as close as possible. The more you visit cron.php, the more accurate cron will be.
Since not everyone has access to cron or Scheduled Tasks on their system, we’ve also added “poormans cron” support. This allows a cron job to be started at certain intervals and works on the basis of visits you get to your site.
To use poormans cron, edit config.php and set:
define('USE_POORMANSCRON', true); define('POORMANSCRON_INTERVAL', 3600);
This will enable the poorman's cron support and set the run interval to once every hour.
Please note that this works based on the visits to your site. This means if you have few visitors, the mechanism will be increasingly inaccurate. With more visitors, it will be more accurate. A normal cron job or sheduled task should always be preferred to the poorman's cron option!
If you receive a permission denied error after starting crontab -e, you may need to use sudo:
sudo crontab -e
You may need to adjust the path to wget, lynx or curl in your crontab. For example, the cron example listed above contains the line:
45 * * * * /usr/bin/lynx -source http://example.com/wolf/app/cron.php
However, Lynx may be in a different location on your server, or not installed at all. To find out where Lynx is installed, enter:
If it is not located at /usr/bin/lynx, adjust the path as needed. The same applies for wget and curl. If none are installed ask a server administrator for help.
It may be necessary to change http://example.com/wolf/app/cron.php to the location of your Wolf CMS installation. For example, if you have Wolf CMS installed in a subdirectory, for example “wolfcms”, it will be http://www.example.com/wolfcms/wolf/app/cron.php).
Taking this approach allows cron.php to be blocked to prevent remote access.
To block remote access to cron.php, in the server, .htaccess or vhost configuration file add this section:
<Files "cron.php"> Order Deny,Allow Deny from all Allow from localhost Allow from 127.0.0.1 Allow from xx.xx.xx.xx <-- your IP address </Files>
If you take this approach to call cron.php, it is probably best not to use the root user to run the cron job. A non-privileged user account, or the Apache account user, for example http-service or www-data, is a better choice. To do so, call crontab -e when logged in as a non-privileged user, or for the Apache account on a Debian server, for example, you can add a user parameter:
sudo crontab -e -u www-data
To setup a Windows machine to run cron.php at a specific time follow the specific instructions below. This can be useful if you are not familiar with Linux/Unix, or if your web host does not offer the ability to run cron jobs; you can run them remotely from your own computer.
Note: These instructions were written for Windows XP but should be similar in other versions of Windows.
- Open Scheduler
- Go to Start > Programs > Accessories > System Tools > Scheduled Tasks
- Double-click Add Scheduled Task
- The Scheduled Task Wizard will appear. Click Next.
- Select the program to run. Choose your browser from the list (for example, Internet Explorer or Mozilla Firefox). Click Next.
- Give the task a Name, such as Wolf CMS Cron Job, and choose the Frequency with which to perform the task (for example, Daily)). Click Next.
- Choose specific date and time options (this step will vary, depending on the option selected in the previous step). When finished, click Next.
- Enter your password if prompted. Change the username if required (for example, you'd like the task to run under a user with fewer privileges security reasons). Click Next.
- On the final page, select the checkbox Open advanced properties for this task when I click Finish and click Finish.
- Go to the task's setting page either by checking the checkbox at the end of the last step, or by double-clicking on the task.
- In the Run box, after the text that is there now (for example, C:\PROGRA~1\MOZILL~1\firefox.exe), enter a space and then type the address to your website's cron.php page in double quotations (for example, C:\PROGRA~1\MOZILL~1\firefox.exe http://www.example.com/wolf/app/cron.php
- To set a frequency more often than Daily (for example, hourly), click the Schedule tab, then click Advanced. Here you can set options such as Repeat task, every 1 hour for 23 hours. Click Ok when finished.
- Change the start time on the task to one minute from the current time. This will allow you to test the task and make sure that it is working.
- When all settings have been configured to your liking, click Apply and OK (note: you may be prompted for your password)
Another way to perform the above commands is by using the schtasks (or at in Windows 2000) command from the command line. To duplicate the example above, which runs Firefox hourly to execute http://www.example.com/wolf/app/cron.php, open a command prompt (Start > Programs > Accessories > Command Prompt) and enter:
schtasks /create /tn "Wolf CMS Cron Job" /tr "C:\PROGRA~1\MOZILL~1\firefox.exe http://www.example.com/wolf/app/cron.php" /sc hourly
Enter your password if prompted.
If for whatever reason you'd rather not deal with a browser window popping up on the machine, you can use wget, the Windows port of which works more or less the same as it does in UNIX. curl and lynx also have windows ports but wget is probably the easiest to set up and use.
Follow the steps for Creating A Scheduled Task above, except select wget.exe as the program to run (you may need to click the Browse button to locate it if you installed from a .zip file, for example). When you get to the Advanced Properties dialog, paste in the following after the program path:
-O - -q -t 1 http://www.example.com/wolf/app/cron.php
Adjust the rest of the options as described above and test it.