Aug 7 Running PHP Scripts with Cron
Lots of programmers like PHP for its ability to code and develop web applications fast. Code-debugging is a lot easier than with PERL or C. However, there is one thing a lot of developers are puzzled about, “How to run PHP Scripts with crontab?”
Cron is normally available on all Unix and Linux distributions; if you cannot access it, contact your root or server administrator. It is a daemon which allows you to schedule a program or script for a specific time of execution. If you want to learn more about cron, click here or type “man crontab” at your command prompt.
I have found myself in the need to run PHP scripts at specific times. For example, to update the content of a website, to remove expired articles, to send out e-mails on a given date and a lot more. While some may think that this is were PHP is doomed, I will show you how it’s done.
A Manual crontab?
The first solution that came to my mind was to run the script directly from my browser (e.g. http://www.mydomain.com/script.php). Since I need to run my script on a regular basis, I squashed that idea. My goodness, all the extra hassle is ridiculous.
An include?
Another possible solution is to include the script in one of the pages of the site, for example the very first: “index.php”. (<? include “cron.php”; ?>)
The drawbacks to this solution are, that it works but when someone accesses the “index.php”. This could cause a lot of extra overhead produced by the script. If you get a lot of traffic, the script is executed 1000 times a day and adds a lot of usage on the database and the server.
On the other hand, if you do not get a lot of traffic, or people tend to access your site over another file, this will not work out as well. If you need to run the script on a regular intervals, this is not a solution.
Crontab!
Let’s suppose you either know what cron is or have read about it using the link above. We want to run our script once a minute. So where do we go from here? Here is how you can accomplish this task.
Your PHP setup
You will need to find out the answer to the following question, “Is my PHP installed as CGI or as an Apache module?”. To find out do the following: Create a new file, name it info.php (just an example), and put in the following code, “<? phpinfo(); ?>”. Upload to your webserver and go to it with your browser.
Now check for Server API (4th item from the top), if it says “CGI”, you have PHP compiled as CGI, if it reads “Apache”, you have it running as an Apache module.
Compiled CGI
If the answer to the question above is “CGI” then you need to add a line to your PHP script. It has to be the first line of your script and must contain your server’s PHP executable location:
#!/usr/local/bin/php -q
That looks a lot like PERL now, doesn’t it? After that let’s add the necessary command to our crontab. Edit /etc/crontab and add the following line:
* * * * * php /path/to/your/cron.php
Execute the following from the command line:
Shell> crontab crontab
Be sure your “script.php” has the necessary permissions to be executable (“chmod 755 script.php”).
Now you are all set!
Apache module
If your PHP is installed using the Apache module, the approach is a little different. First, you need access to Lynx (Lynx Browser for more information). Lynx is a small web browser, generally available on Unix and Linux.
Running your PHP script will not require you to add any additional lines. You simply have to edit your /etc/crontab file and add the following line:
* * * * * lynx -dump http://www.somedomain.com/cron.php
Please note that in general, you have to specify the entire URL (with “http://” and so on). But depending on your Lynx’s configuration, the URL might be relative; I suggest always using the absolute reference as in my example above – it always works.
Again execute the following from the command line:
Shell> crontab crontab
That all it takes to get a cron job setup using PHP. Hope you have learned something new and will use it to save overhead time on the server and on the developer.
Related posts:







[...] Running PHP Scripts with Cron By till Lots of programmers like PHP for its ability to code and develop web applications fast. Code-debugging is a lot easier than with PERL or C. However, there is one thing a lot of developers are puzzled about, “How to run PHP Scripts with … – http://htmlcenter.com [...]
[...] Read the rest of this great post here [...]
There is another way that seems to be independent of whether php is installed as a module or cgi…use curl or wget.
* * * * * /usr/bin/curl -o http://www.yoursite.com/path/to/script.php
or
* * * * * /usr/bin/wget -q -O /dev/null http://www.yoursite.com/path/to/script.php
Just wanted to throw that out there since it seems some jobs on certain webhosts just don’t work with either previous option.
@dave.
Nice one, if like me you don’t have cgi version of PHP, or lynx installed wget does the business – thanks for throwing it out, caught with gratitude!
[...] Courtesy : Running PHP Scripts with Cron | HTMLCenter – Web Development Blog [...]
As for me it is very unusuall to use such script
Nice one., Yeah! This allows you to run your PHP scripts at command line similar to Perl or any other scripting language. No need to learn another scripting language such as Perl, Bash or Awk.
None of these work for me!
What’s up with that?
If I bring up the “page” (php script) in my browser, from a different machine (windows) it runs and does its job. But if I try any of the 3 above methods, nothing appears to happen. No error messages, nothing.
I’m running Ubuntu and the script was created by phpMyBackupPro.
@MrSato – Is your PHP installation running as a CGI module or an apache module?
Do you know if you have any sort of GUI front-end installed to help you manage crontabs? On my personal server, we’re running Webmin, which allows you to easily add crontabs to your server.
Are you able to run PHP from the command-line? If so, try running your PHP script from the command-line and see if it works. If so, then the cron job should run. If not, then you’re going to need to find a different way to run the cron job.
@MrSato – Hi
do you the lynx installed on your server, if not then installed it
then to run the cron tab just
lynx -dump “http://www.yoursite.com/path/to/script.php”
Hope this will help.
Regards
Mahesh Vanneldas
#10 solution worked perfectly for me after trying many many other methods. Although I sdidn’t use “” around the url
You need to add the run times at the beginning of each line e.g.
5 * * * * wget etc…
or
1/ * * * wget will run every minute etc..
Check also my tutorial – maybe you’ll find it useful: http://www.lampdocs.com/blog/2008/09/18/how-to-run-a-php-file-using-crontab/
Thanks!
Hi
I’m new to this “cron” thingi.
Can you recommend where to learn more about it?
Searching the net only confusing me more.
Thanks,
Amit.
Hiya,
is there any solution to runa .php via cron, which is NOT accessible through web?
Cheers!
@Vanquish – If you have access to set up actual crontabs through your server, it is absolutely possible. You can place a PHP script anywhere on the server. As long as you have the command-line interface (CLI) extension for PHP installed, you can tell the crontab to execute the PHP script.
@MrSato – Is your PHP installation running as a CGI module or an apache module?
Thanks for the advice. The Apache module command was exactly right!
A thousand thank you’s. Great article, exactly what I was looking for to run a PHP script once a day.
[...] This class and script makes it easy to turn a one-shot php script into a daemon that runs in the background on a timer. This is good for things you want to happen infrequently. For example, your site may need to use curl to fetch several different rss feeds, combining them into one feed. You don’t want to fetch all the feeds every time someone asks for the combined one. It is better to generate the combined feed every 5 minutes or so. [...]
I need to run a cron for my auto-responder and the setting for the auto-responder are set to deliver the message every 10 minutes but when I set the cron it doesn’t seem to be working.
This is the path /usr/bin/php -f /home/mysite/public_html/AR/admin/cron/cron.php
This is the cron setting that was provided by the AR script and it’s supposed to run according to my settings in the AR Admin section.
What is the purpose of the -f ?
What would the wildcard settings be if I want the cron to run every 10 minutes?
If none of these work for you, you can use wget
wget –output-document=temp http://yourname.com/file.php
wget just downloads the file. It is requesting it over http, so the webserver executes the script first (which is what you want).
“–output-document=temp” saves the file as “temp” and overwrites the old one each time, so you don’t end up with several copies. You could also just save it to the temp directory, then it is deleted automatically.
This does require that the script be somewhere that is accessible by the public, but it should work no matter how php is setup, and whether or not lynx is installed
You’ll still need to setup the crontab, but using this command will work
I’m using this
* * * * * /usr/bin/wget -q -O /dev/null http://www.yoursite.com/path/to/script.php
and it works fine in command line, but not when i try to use it by cron. Does anyone know whats wrong? Looks like cron never does the job..
I have solution.. Add one whitespace after the line in the crontab file
Hey Amit:
Hoping you found the solution to your problem by now, but for other who are struggling with Cron, consider using an online cron replacement service to run your cron job PHP scripts. I personally recommend http://cronless.com because they’re reliable, easy to use, friendly and stable.
They offer free cron jobs and site monitoring with a reasonably priced upgrade. Site monitoring too. Great service. Check them out.
Iam customizing vtiger CRM. There are following users in the system based on the heirarchy:
Technician – Territory Manager – Area Sales manager – MD
Trouble ticket is created by Technician is assigned to Territory manager. Territory manager has 30 min to respond to the same. If he fails to, it automatically escalates to Area Sales Manager. The same time limit applies to him after which it gets escalated to the MD. the MD should be able to view all the details along with the date and time.
a cron script that can scan all open trouble tickets should work . Based on the last-modified time and current time difference I have set the assigned_user_id to right user-id. But, it is not working. I would really appreciate your help.
Regards,
Nitesh
I want to create cronjobs for the 2 following files,
include/CronStats.php
include/AlexaThumbCronJob.php
Plz reply if you have a idea how i can create this
thanks a lot
@akou I use cronless.com to automate php scripts that validate links in my web directories, its been working perfectly well for the past 3 months.