≡ Menu

PHP Cron Job: How to Execute PHP Script Using Crontab in Linux

Question: I have a php script on my server that can be execute from the command line and also can be accessed from the browser using Apache web server. I would like to execute execute this php script every 1 hour. How do I schedule this as php cron job on Linux environment.

Answer: Use one of the following methods to schedule your php script as linux crontab.

Method 1: Execute the script using php from the crontab

Just like how you call your shell script (As show in our crontab 15 examples article), use the php executable, and call the php script from your crontab as shown below.

To execute myscript.php every 1 hour do the following:

# crontab -e
00 * * * * /usr/local/bin/php /home/john/myscript.php

Method 2: Run the php script using URL from the crontab

If your php script can be invoked using an URL, you can lynx, or curl, or wget to setup your crontab as shown below.

The following script executes the php script (every hour) by calling the URL using the lynx text browser. Lynx text browser by default opens a URL in the interactive mode. However, as shown below, the -dump option in lynx command, dumps the output of the URL to the standard output.

00 * * * * lynx -dump http://www.thegeekstuff.com/myscript.php

The following script executes the php script (every 5 minutes) by calling the URL using CURL. Curl by default displays the output in the standard output. Using the “curl -o” option, you can also dump the output of your script to a temporary file as shown below.

*/5 * * * * /usr/bin/curl -o temp.txt http://www.thegeekstuff.com/myscript.php

The following script executes the php script (every 10 minutes) by calling the URL using WGET. The -q option indicates quite mode. The “-O temp.txt” indicates that the output will be send to the temporary file.

*/10 * * * * /usr/bin/wget -q -O temp.txt http://www.thegeekstuff.com/myscript.php

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 15 comments… add one }

  • yoander July 8, 2011, 7:14 am

    Another way around:
    # chmod a+x /home/john/myscript.php
    # crontab -e
    00 * * * * /home/john/myscript.php

  • Tapas Mallick July 8, 2011, 8:59 am

    Typo: “-O /dev/null” should be replaced by “-O temp.txt”

  • blackcat November 24, 2011, 2:44 am

    hello ,your articles are so good.

  • gredo January 17, 2012, 4:50 pm

    Hi guys,
    I have a serious issue with running cron command. My host not supported format below mentioned, just the URL.)http://www.mysite/example.php ( I’m affraid a bit, the host do not going to support the shell as well…anyway)

    Somehow I need to „wrap” this command, to able to call the cron:
    php “/web/mysite/mysite.com/administrator/components/comex/helpers/cron.php” username=”admin” passwd=”adminpass” template_id=”50″
    I’ve absolutely no experience, how can do this.
    Could somebody help me out, how can I solve?

    Thanks in advance

  • yoander January 19, 2012, 1:50 pm

    Can you explain a little better what do you want to do?

  • justanobar January 31, 2012, 9:16 am

    I did not get it to run the php with php cgi (seems like used bash?)

    so this worked:

    /usr/bin/php -q /var/www/myhome/public_html/myscript.php

  • azam August 23, 2012, 5:48 pm

    Pls insert ‘#!/usr/bin/php’ on the top of your php program, if you want to run it on shell command line.

    Pls. refer to ‘yoander’ reply on July 8, 2011 at 7:14 am

  • narnedra September 26, 2012, 4:25 am

    Hai , this is narendra
    i am using Ubuntu 12.04 LTS and in that im using XAMPP 1.7.7

    i want to run the following single line command through webserver
    “/facedetect –cascade=/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml –scale=1.5 image.jpg”
    it couldn’t executed .
    .facedetect is a programme that will detect a face in given input image
    the whole process is run through web browser

    It shows the error number 32767 in browser. so please help me.

  • Mike September 27, 2012, 1:19 am

    Great article. Thanks!
    I’m currently using wget and it works fine, but it counts as an HTTP request in my web stats, and it’s really skewing my stats since the cron runs ever minute.

    Do any of the other methods not trigger a HTTP request?

  • ashik September 27, 2012, 4:15 am

    I own a Linux server, and wanna send birthday greetings to the registered users.
    I have written a php code to send email. the file name is send_greetings.php
    I have set the job timing( the day, time, hour, mins etc)
    Now what do I write in the command line of CRON JOB to execute the php file ?

  • Muchi December 6, 2012, 7:40 am

    Thanks, this really helped

  • Naveed December 7, 2012, 5:04 am

    hi ashik
    did you got your desired commond. I have also a php file like yours and i want to execute it through CRON JOB. if you got it then kindly share it with me here

  • Ashik December 9, 2012, 5:37 am

    Hi Bro Naveed yes I got my issue solved, Its simple.

    1. In the Cron Job option of control panel give an email address
    2. Select the time you wan’t to execute the file
    3. In the command box simply paste the following code

    lynx -source http://websitename/foldername/filename.php > /dev/null

    You are done.

    > /dev/null is used to deactivate notifications to the email address you give. If you want notifications everytime cron executes you can remove < /dev/null


  • Aftab February 10, 2014, 6:58 am

    gives the following error when installing the cron job for the system.

    ow_includes/config.php file is incorrect. Update it with details provided below.

  • Tushar May 5, 2014, 4:09 am

    It gives output like below:
    Warning: Failed to create the file temp.txt
    Which path should be there for temp.txt? If I only input temp.txt, in which directory it stores?

Leave a Comment