Introduction to Cron Jobs
Overview
Cron jobs are automated tasks executed by a server at scheduled intervals. Sugar relies on cron to perform time-based jobs like sending email reminders, processing workflow events, and monitoring forecast intervals, just to name a few. This article explains how to set up cron on the server where you host Sugar on site.
For more information on the default scheduled jobs that rely on cron in Sugar, please refer to the Schedulers documentation.
Windows
First, you must create a batch file.
- Call it
sugar.bat
and put it in the c:\ directory - Assuming your php.exe binary is located at
c:\Program Files\PHP\php.exe
- and your php.ini is located in
c:\Program Files\PHP\php.ini
- and that you've installed Sugar in
c:\inetpub\wwwroot\sugarcrm
- and you are running PHP
These values are most likely are different for you, so please plan accordingly. But under those assumptions, your batch file will look something like:
cd C:\inetpub\wwwroot\sugarcrm
c:\Program Files\PHP\php.exe -c C:\Program Files\PHP\php.ini -f cron.php
Basically, this causes us to switch to the Sugar directory and, using the full path, call php.exe
with the -f
argument to run cron.php
, which is a file provided by Sugar. This file will, among other things, cause your e-mail to be checked. So now we have a batch file to run, but how do we run it? You need to create a Scheduled Task via the Windows Command Prompt. Here is the syntax used to run our batch file every 1 minute:
schtasks /create /sc minute /tn "Sugar Cron" /tr c:\sugar.bat
Linux
It's a bit easier on Linux. First, you must edit the web server user's "crontab" and provide information that will enable the command to run. I will assume our web server user is "apache" for this example:
crontab -e -u apache
(insert the following values into the resultant editor)
* * * * * cd /var/www/html/sugarcrm; php -f cron.php > /dev/null 2>&1
Note that it may be necessary to specify the path to the php.ini file. Some PHP distributions use separate php.ini files for Apache and command-line. Example:
* * * * * cd /var/www/html/sugarcrm; php -f cron.php > /dev/null 2>&1
Mac
Apple relies on the launchd framework, which includes cron-like functionality, though it is broader in scope. It can also be used to coordinate Sugar's scheduled jobs. It operates on XML files called "plists," which are stored in strategic locations on the filesystem. You can create these XML/text files using your favorite editor.
vi /Library/LaunchDaemons/com.sugarcrm.Scheduler.plist
Insert the following code into this file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.sugarcrm.Scheduler</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/curl</string>
<string>http://www.example.com/crm/cron.php</string>
</array>
<key>RunAtLoad</key>
<true />
<key>StartInterval</key>
<integer>1</integer>
</dict>
</plist><?xml version="1.0" encoding="UTF-8"?>