41

I want to run PHP scripts automatically on a schedule. I learned about CRON recently. But I don't know how to install and use it.

I'm using PHP, CSS, HTML, and running on XAMP apache server on localhost. How do I install and use Cron?

1

5 Answers 5

58

Do you have a Windows machine or a Linux machine?

Under Windows cron is called 'Scheduled Tasks'. It's located in the Control Panel. You can set several scripts to run at specified times in the control panel. Use the wizard to define the scheduled times. Be sure that PHP is callable in your PATH.

Under Linux you can create a crontab for your current user by typing:

crontab -e [username]

If this command fails, it's likely that cron is not installed. If you use a Debian based system (Debian, Ubuntu), try the following commands first:

sudo apt-get update
sudo apt-get install cron

If the command runs properly, a text editor will appear. Now you can add command lines to the crontab file. To run something every five minutes:

*/5 * * * *  /home/user/test.pl

The syntax is basically this:

.---------------- minute (0 - 59) 
|  .------------- hour (0 - 23)
|  |  .---------- day of month (1 - 31)
|  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ... 
|  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat 
|  |  |  |  |
*  *  *  *  *  command to be executed

Read more about it on the following pages: Wikipedia: crontab

10
  • 3
    Cron isn't installed by default on all GNU/Linux systems (as crazy as that may sound). You might add a note about how to install a cron (e.g. cronie, vixie-cron...) and how to get it to run on startup (chkconfig on Redhat systems...). Nov 26, 2009 at 8:54
  • Good suggestion. Will edit the post a bit :). Although it's difficult to guess without knowing what Unix ;-) Nov 26, 2009 at 8:55
  • I had one doubt. Installing CRON in my local windows system will reflect on my hosting web server???. Plz answer me
    – Rajasekar
    Nov 26, 2009 at 9:09
  • What Operating System does your hosting server run on? Nov 26, 2009 at 9:20
  • So my local system should be ON at the scheduled time and should connected to internet. Is it so????
    – Rajasekar
    Nov 26, 2009 at 13:01
27

Install cron on Linux/Unix:

apt-get install cron

Use cron on Linux/Unix

crontab -e

See the canonical answer about cron for more details: https://serverfault.com/questions/449651/why-is-my-crontab-not-working-and-how-can-i-troubleshoot-it

3

On CentOS/RHEL:

yum install cronie
3

Installing Crontab on Ubuntu

sudo apt-get update

We download the crontab file to the root

wget https://pypi.python.org/packages/47/c2/d048cbe358acd693b3ee4b330f79d836fb33b716bfaf888f764ee60aee65/crontab-0.20.tar.gz

Unzip the file crontab-0.20.tar.gz

tar xvfz crontab-0.20.tar.gz

Login to a folder crontab-0.20

cd crontab-0.20*

Installation order

python setup.py install

See also here:.. http://www.syriatalk.im/crontab.html

0
-3

Cron is so named "deamon" (same as service under Win).

Most likely cron is already installed on your system (if it is a Linux/Unix system).

Look here: http://www.comptechdoc.org/os/linux/startupman/linux_sucron.html

or there http://en.wikipedia.org/wiki/Cron

for more details.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.