About Linux FAQ

Browse More FAQs:

Linux / UNIX Setup and run php script as a cron job

Posted by Vivek Gite [Last updated: July 11, 2007]

Q. I’ve nicely written PHP script that I’d like to run as a cron job. I’m using CentOS 4.5 as server with Apache web server. How do I setup a PHP script as a cron job?

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

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
#!/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
Output:

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

Save and close the file.

Subscribe to our free e-mail newsletter or RSS feed to get all updates. You can Email this page to a friend.

Related Other Helpful FAQs:

Discussion on This FAQ

  1. MDZakri Says:

    Thanks Pal,

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

  2. Swapneel Says:

    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.

Leave a Reply

We encourage your comments, and suggestions. But please stay on topic, be polite, and avoid spam. Thank you very much for stopping by our site!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word

Tags: , , , , , , , ,

Copyright © 2006-2008 nixCraft. All rights reserved - TOS/Disclaimer - Privacy policy - Sitemap - Powered by Open source software.