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..
|
|
|
|











My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you or help you resolve a problem. Read more about
{ 6 comments… read them below or add one }
Another way around:
# chmod a+x /home/john/myscript.php
# crontab -e
00 * * * * /home/john/myscript.php
Typo: “-O /dev/null” should be replaced by “-O temp.txt”
hello ,your articles are so good.
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
Gredo
Can you explain a little better what do you want to do?
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