Linux / UNIX Setup and run php script as a cron job
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:
- Ubuntu Linux how to set the date and time via Network Time Protocol (NTP)
- Run crontab Every 10 Minutes
- Tweaking php for maximum execution time for script
- How do I add jobs to cron under Linux or UNIX oses?
- Linux Verify crond Daemon And Cronjobs Are Running
Discussion on This FAQ
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!
Tags: access_permissions, apache_web_server, CentOS, chmod_command, cron_job, Linux, PHP, php_script, UNIX



July 26th, 2007 at 5:30 am
Thanks Pal,
Its teach me more on setting the crontab on different server
July 31st, 2007 at 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.