• Home

Logo

Navigation
  • Home
  • Articles
    • Content Writing
    • Design
    • General
    • Internet Marketing
    • Social Media
    • Tools and Tips
    • Usability
    • Web Hosting Articles
  • Tutorials
    • AJAX Tutorials
    • ASP Tutorials
    • C# Tutorials
    • CGI and Perl Tutorials
    • CSS Tutorials
    • Flash Tutorials
    • HTML Tutorials
    • Illustrator Tutorials
    • Java Tutorials
    • JavaScript Tutorials
    • Linux Tutorials
    • Miscellaneous Tutorials
    • MySQL Tutorials
    • Photoshop Tutorials
    • PHP Tutorials
    • Python Tutorials
    • Wireless Tutorials
    • WordPress Tutorials
    • XML Tutorials
  • Scripts
    • AJAX Scripts
    • ASP Scripts
    • ASP.NET Scripts
    • CGI & Perl Scripts
    • Flash Scripts
    • Java Scripts
    • JavaScript Scripts
    • PHP Scripts
    • Python Scripts
    • Remotely Hosted
    • Tools and Utilities
    • XML Scripts
  • Answers
  • Online Services
  • Tools

Running PHP cron jobs – regular scheduled tasks in PHP

By Akash Mehta | on May 15, 2008 | 6 Comments
PHP Tutorials
  • Tweet
  • Share
  • Tweet
  • Share

Scheduled tasks are a fairly common feature in modern web applications. From cleaning out caches every 24 hours to checking subscription periods and even generating reports, more web applications live by the clock than ever before. But how do we schedule the execution of a PHP script on the server side?

The answer, for Linux-based servers, is crontab. The crontab utility on UNIX systems allows commands to be executed and predefined intervals. These commands are essentially the same commands as you would run from any local console. For example, you could create a new cron “job” to run a bash script, and set it to run once per day at 12:00 midnight.

In terms of PHP, we can then call our PHP scripts via command line using the `php` binary. There are two issues here: first, we need to know where PHP is, and second, we need to know where our scripts are. Say your scripts are in /home/username/example.com, and your PHP binary is in /usr/bin, Your command to run your script could be this:

/usr/bin/php /home/username/example.com/myscript.php

Now, we have to configure cron to execute this. If your host has cPanel, you may have a cron job interface which will take this command as an option and allow you to configure visually the times at which it will execute. If you don’t have some kind of web-based interface, you’ll need to go one step further and manually create crontab entries via a command line terminal.

Fire up a console window at your server (e.g. in PuTTY) and run “crontab -e”. If you are prompted for an email address, enter an address you can easily manage e.g. a gmail inbox or a catch-all. A command-line text editor such as nano or vi/emacs should appear, and you’re ready to start creating crontab entries.

Here’s a sample entry:

0 0 * * * /usr/bin/php /home/username/myscript.php

The first section is the key: this defines the timing. There are five fields, minute, hour, day of month, month and day of week. Each takes a numerical value (typically starting at 0, not 1) or an asterisk to represent “all values”. Ranges are also an option, e.g. 1-5. For example, here we want the 0th minute of the 0th hour of every day of month, every month, on every day of week – in otherwords, midnight every day. This page has a great tutorial on cron scheduling.

Once you’ve worked out the cron entry for your intended timing, just add the entry to your crontab file – edited by running `crontab -e` – and save and exit. Crontab will automatically manage the changes and start scheduling your tasks immediately. If your scripts have any output, you will also receive an email with the output. Practise this a little, and within no time you’ll be a PHP task scheduling master!

Share this story:
  • tweet

Author Description

6 Responses to “Running PHP cron jobs – regular scheduled tasks in PHP”

  1. May 16, 2008

    Craig Log in to Reply

    You can also add ‘#!/usr/bin/php -q’ as the first line inside the php script.
    Chmod the php file as executable, then it will be executable like any other script.

    0 0 * * * /usr/bin/php /home/username/myscript.php

    turns into:

    0 0 * * * /home/username/myscript.php

  2. May 20, 2008

    pauluZ Log in to Reply

    Don’t forget to add a parametr -c with the path to php.ini file in the first line inside the php script. Why?
    Sometimes the conf file for php isn’t loaded and then you have to point to it:
    `#!/usr/bin/php -q -c /etc/php.ini’

  3. July 25, 2009

    gaditano Log in to Reply

    Hello,

    I’m using the cron but I have a question.
    If a run the script from the cron, I’m receiving the message:

    Fatal error: Allowed memory size of 16777216 bytes exhausted (tried to allocate 45 bytes) in /home/jgodino/public_html/sginfra.com.ar/sginfra_system_files/plugins/dompdf/include/frame.cls.php on line 358

    If I run the same scrip from the browser, the program run ok.
    This run ok, because the script is reading the php.ini with 64MB.

    Is the cron using another php.ini?

    Thanks

    Gaditano

  4. November 25, 2009

    Sanuj Log in to Reply

    Hello!

    My php file is located at /var/www/html/ddic/ad/a_index.php and php.ini is at /etc/php.ini

    i have kept this line at /etc/crontabl and created a ddic file in cron.daily to execute

    30 2 * * * root run-parts /etc/cron.daily

    and ddic file as

    #!/bin/sh
    30 2 * * * * /usr/bin/php /var/www/html/ddic/ad/a_index.php

    Will This execute the php file (a_index.php) daily at 2:30 AM.

    Please suggest me.

  5. January 30, 2010

    adbox Log in to Reply

    I use an external cron management service (free) http://www.cronwatch.com

  6. March 24, 2010

    atasözleri Log in to Reply

    Thank you but i dont understand this method.

    I am using this :

    exec( ‘php /home/username/routine_task.php > /home/username/routine_task.log &’ );

You must be logged in to post a comment.

Connect With Us

RSSSubscribe 0Followers 494Likes
  • Popular
  • Recent
  • Comments
  • Creating Energy Spheres in Photoshop

    Apr 15, 2008 - 96 Comments
  • Easy Screen Scraping in PHP with the Simple HTML DOM Library

    Aug 6, 2008 - 20 Comments
  • Calculating date difference more precisely in PHP

    Mar 7, 2008 - 13 Comments
  • When Does Hosting Your Website in the Cloud Make Sense?

    Oct 8, 2010 - 2 Comments
  • Fun with the Microsoft Managed Extensibility Framework Part 2

    Oct 6, 2010 - 0 Comment
  • Fun with the Microsoft Managed Extensibility Framework Part 1

    Sep 22, 2010 - 0 Comment
  • Website Management on the go with the iPad

    I appreciated your post, but I was looking for something I didn't...
    November 24, 2012 - drmoderator
  • Creating Energy Spheres in Photoshop

    I'm a little stuck down here especially at the step of creating the...
    November 23, 2012 - sarah
  • Running background processes in PHP

    Can you give an example? As see it, you can use this only when you...
    November 16, 2012 - Shaked Klein Orbach
Developer Resources
  • Tutorial Directory
  • Learn HTML
  • Learn PHP
  • Learn CSS
  • Learn AJAX
  • Learn JavaScript
  • Learn Pear
  • White Papers
  • Resources
    • NetVisits Web Directory
    • Realtor Pixels
    • Answers On The Run
    • Ask A Geek
  • Recent Posts

    • When Does Hosting Your Website in the Cloud Make Sense?
    • Fun with the Microsoft Managed Extensibility Framework Part 2
    • Fun with the Microsoft Managed Extensibility Framework Part 1
    • Website Management on the go with the iPad
    • Code Contracts in C# 4.0 – Part 1

    Calendar

    May 2013
    M T W T F S S
    « Oct    
     12345
    6789101112
    13141516171819
    20212223242526
    2728293031  

    Recent Comments

    • drmoderator on Website Management on the go with the iPad
    • sarah on Creating Energy Spheres in Photoshop
    • Shaked Klein Orbach on Running background processes in PHP
    • Thomas Cuvillier on How To Upload Files Using PHP
    • rizal aditya on Extracting text from Word Documents via PHP and COM
    • Home
    © 2003 - 2013 DeveloperTutorials.com. All Rights Reserved. Privacy Policy.