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. [donotprint]
Tutorial details
Difficulty Easy (rss)
Root privileges No
Requirements None
Time 1m
[/donotprint] 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):

#!/usr/bin/php

If you are using FreeBSD unix os, use:

#!/usr/local/bin/php

#!/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.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
15 comments… add one
  • MDZakri Jul 26, 2007 @ 5:30

    Thanks Pal,

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

  • Swapneel Jul 31, 2007 @ 21:06

    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 Jun 4, 2009 @ 18:51

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

  • 🐧 nixCraft Jun 4, 2009 @ 20:41

    @Travid and Swapneel

    Thanks for the heads-up!

  • sricha Oct 23, 2009 @ 17:33

    Thanks. It was really helpful for me.

  • azaazaaza Feb 8, 2010 @ 0:00

    Big big big big thanks!!!

  • smerez23 Apr 3, 2010 @ 21:56

    i tried passing arguments to a php script in UNIX

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

  • smerez23 Apr 3, 2010 @ 21:57

    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 Feb 15, 2011 @ 12:31

    I like this for running daily cron handling thanks :)

  • TonyRiver Jul 9, 2011 @ 1:40

    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

    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 Jul 2, 2012 @ 11:25

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

  • Damodar Aug 24, 2012 @ 7:53

    thanks a lot.

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

    it run.

    thanks

  • Hendrik Jan 11, 2013 @ 10:28

    Thanks, great help

  • Dr. H K Sahjwani Apr 4, 2014 @ 5:53

    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?
    Sahjwani

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.