≡ Menu

Linux / UNIX Setup and Run PHP Script As A Cron Job

I‘ve written a PHP script. I would like to run it as a cron job. I’m using CentOS server with Apache web server. How do I setup a PHP script as a cron job on a Linux or Unix-like systems?

You need to setup execute permission on a php script. You need to use chmod command to change file access permissions. Use the chmod command as follows on your php script:

chmod +x scriptname.php

Also make sure you add following line to top of php script (first line should be #!/usr/bin/php):


If you are using FreeBSD unix os, use:


#!/usr/bin/php is called as a shebang (pound bang). It execute php script using the interpreter /usr/bin/php. Save and close the file.

Setup and run php script as a cron job

Now add cron job by typing following command:
$ crontab -e
Sample outputs:

# run everday at 10:45
45 10 * * *     /path/to/myphpscript.php

Save and close the file.

Share this tutorial on:
{ 15 comments… add one }
  • MDZakri July 26, 2007, 5:30 am

    Thanks Pal,

    Its teach me more on setting the crontab on different server :)

  • Swapneel July 31, 2007, 9:06 pm

    My apologies for nitpicking but shouldn’t the cron job be,

    45 10 * * * /path/to/myphpscript.php

    for the Cron to run at 10:45 as the first field from the left starts with Minute..Hour and so on.

  • Travis June 4, 2009, 6:51 pm

    Swapneel is correct. A cronjob runs off of the following format for schedules.
    minute hour day month dayofweek command

  • nixCraft June 4, 2009, 8:41 pm

    @Travid and Swapneel

    Thanks for the heads-up!

  • sricha October 23, 2009, 5:33 pm

    Thanks. It was really helpful for me.

  • azaazaaza February 8, 2010, 12:00 am

    Big big big big thanks!!!

  • smerez23 April 3, 2010, 9:56 pm

    i tried passing arguments to a php script in UNIX

    For example: 45 10 * * * /path/to/myphpscript.php -args filename=”hello.txt”

  • smerez23 April 3, 2010, 9:57 pm

    is there a way to pass arguments to a php script in unix

    For example: 45 10 * * * /path/to/myphpscript.php -filename=”hello.txt”

  • Rupesh February 15, 2011, 12:31 pm

    I like this for running daily cron handling thanks :)

  • TonyRiver July 9, 2011, 1:40 am

    Great, thanks. Been do lazy to read up on this for a while, but this page makes it simple.

  • chamil May 31, 2012, 10:21 am

    I run as a ./email_test.php

    before I set as a cron job I run on a terminal,but then I had following error

    “/usr/bin/php^M: bad interpreter: No such file or directory centos PHP

  • dimti July 2, 2012, 11:25 am

    Search your php interpreter with help this command:
    locate bin/php
    if you need update location database, use:
    or (if it FreeBSD)

  • Damodar August 24, 2012, 7:53 am

    thanks a lot.

    i run cron 00 11 30 08 4 php /path /script -args argumentName=”value”

    it run.


  • Hendrik January 11, 2013, 10:28 am

    Thanks, great help

  • Dr. H K Sahjwani April 4, 2014, 5:53 am

    I have a website http://www.noidasindhis.com from which I send emails to the registered Sindhis.
    My program to send emails is in PHP and runs fine.
    The site has been taken from dollar2host under Linux environment. I wish to send my mails at specified intervals using cron job but do not know the Linux command to do so.
    Can any one help me?

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: ,