Schedule Automatic Cron Jobs of *.PHP Scripts
As a hacker, I have to program my remote web server to automatically perform routine tasks for me. It's a whole lot easier and cheaper to rely on my web hosting company to manage an always-on server. For starters, I don't have to pay for the 24/7 electricity the on-call technician!
To make the server perform an automated task in a Linux or Unix environment, you need to schedule a cron job. I won't go through all the details of setting it up in shell terminal since most people do not have remote shell access. You can read about the details more at the cron and crontab Wikipedia articles. Most of us deal with an interface on the cPanel that looks kind of like one of these two screens.
Do I Run the PHP Text File or a Program?
Scheduling can be easy enough if you follow the on-screen instructions. But when the form prompts you for the command what do you input? The first impulse would just be to feed the URL or file path of the PHP file to cron. That will not work. To cron and to Linux, a PHP file is just a simple text file. Cron needs a program to parse, interpret, and run the commands in the text file. In a cron job, you need to specify a script or an executable file to run. That's easy enough if you know how to write Unix scripts. But, I sure do not have any idea! I only know how to program in PHP, HTML, JavaScript, and a little C/C++. For web programmers with the knowledge of modern server side scripting like PHP, there is hope.
There are three ways you can do this. The first way is to actually call the PHP interpreter binary to run the PHP script. The second way is to call a text-based browser for terminal environments. The last way is to call an HTTP retriever. To see which one works for you, create a PHP file that emails your Inbox when it is successfully executed.
-
span style="color:#FF9933; font-style:italic;">//Filename: cron.php
-
"my@email.com", "Cron Successful!", "Hello World!"
PHP Interpreter
This will only work if your server parses PHP files as a CGI directive. That is, PHP is not installed as an Apache module. Most newer web server setups utilize PHP via the Apache module since it is a more secure setup. Therefore, this may not apply to you. If you want to test it out, cron the above PHP script with this:
/path/to/php /path/to/your/cron.php
or
/path/to/php http://www.mydomain.com/path/to/your/cron.php
Text Browser
If referring to the PHP interpreter does not work, you will need to call a text web browser to open the file, thus forcing the HTTP daemon to run the script. There are many text browsers out there. The most popular one is Lynx. It should be included on most *nix environments.
/path/to/lynx http://www.mydomain.com/path/to/your/cron.php
HTTP Retrievers
Apparently, my web host removed the lynx command so I was forced to look elsewhere. My last resort was HTTP retrievers like "get" and "wget." Basically, these binaries "get" the file from the HTTP daemon like text browsers and usually save it somewhere.
/path/to/get http://www.mydomain.com/path/to/your/cron.php
or
/path/to/wget http://www.mydomain.com/path/to/your/cron.php
Good luck! If you have any question or suggestions, please leave a comment!
April 20th, 2007 at 1:50 am
Nice round up of options.
If I may add a couple of points:
On my host (and presumably others) using wget stores the file in my home dir. For scripts that run once a day this can quickly lead to loads of files that need to be cleared out. Routing the output of cron to /dev/null (e.g. wget http://www.domain.com/myscript.php > /dev/null & 2 ) prevents this.
Also depending on your setup you can call php as a script (just like any other shell script) by adding the shebang #!/path/to/php to the top of the file and chmoding it to be executable. You can then execute it directly.
Rob
April 20th, 2007 at 8:20 am
Thanks for your suggestions. The thought of HTTP retrievers dumping the output in a file did not even cross my mind. Thanks for mentioning it. I guess I better check my home directory if it is bloated with the byproduct of my cron jobs! 🙂
November 27th, 2007 at 9:21 pm
How do I execute the CRON job of PHP script on Windows? I'm newbie.
April 20th, 2008 at 12:13 am
Farreina, I am also want to know to do it for windows. Any body there who can help us?
Regards,
Manish Patel
August 23rd, 2008 at 4:56 pm
I tried with cpanel Cron Jobs. but it send a email with access denied.
Why this happend?
September 29th, 2008 at 10:19 am
Under windows you should add a new tas in task sheduler:
\
September 29th, 2008 at 10:21 am
ok, somebody cut my text
task: path.to.browser.exe http.path.to.cron.php
August 26th, 2013 at 11:47 am
I for all time emailed this blog post page to all my friends, as if like to read it next
my contacts will too.
October 17th, 2013 at 6:26 am
Wow, this post is good, my sister is analyzing these things,
so I am going to tell her.
October 18th, 2013 at 8:57 am
I do agree with all the concepts you've introduced
to your post. They are really convincing and can certainly work.
Still, the posts are very short for newbies. May just you please extend them
a bit from next time? Thank you for the post.
October 18th, 2013 at 3:40 pm
I think everything posted made a lot of sense.
But, think on this, suppose you composed a catchier post title?
I am not suggesting your information is not good,
however suppose you added a headline to possibly
get a person's attention? I mean Schedule Automatic Cron
Jobs of *.PHP Scripts - hacker not cracker is kinda vanilla.
You ought to glance at Yahoo's front page and note how they create article titles to grab viewers interested.
You might add a video or a related picture or two to get people interested about everything've got to say.
Just my opinion, it would make your website a little livelier.
October 29th, 2013 at 11:01 am
Steven Scott is a Maui-based freelance travel writer and photographer and can be reached at thehawaiitourist@gmail.
Once they are VPi - X certified, we can then get them to service our national accounts.
The perfect escape can be had at this quiet, condo-type resort with sprawling grounds right in front of Kaanapali Beach.
November 11th, 2013 at 1:35 pm
About | خريد vpn| خريد کريو| خريد ساکس| خريد وي پي ان...
I saw this really great post today....
November 26th, 2013 at 3:34 am
I am curious to find out what blog platform you have been working with?
I'm experiencing some minor security issues with my latest site and I'd like to find something more secure.
Do you have any suggestions?
January 20th, 2014 at 12:13 pm
If you need more information of the above subject, please
visit my home page at:. In his clinic, he used suspension from pulleys, racks, moving manipulation and different uncommon merchandise for addressing rheumatism and inflammatory disease, basic cognitive process that rheumatism originates from your vertebrae.
Catch more carp and beat leading brand readymade boilies
with your own winning versions.
January 23rd, 2014 at 11:50 pm
It is a dead air space which provides the greatest insulating capacity.
Soft, silky skin is always nicer than being hairy and
dry like a caveman. Calling up your supervisor outside of work hours becomes inevitable once in a while.
Know the difference between what makes a good salesperson for your product and what makes a good
salesperson for your company"s long-term future. You start asking yourself constantly, how can this get done without me.
Also visit my web site - colocation hosting web (http://dapalma.co.uk/profile/curt93e)
January 23rd, 2014 at 11:54 pm
Many people are already making the leap with
renewable energy sources and downsizing their 'needs.
He had no knowledge that the aged guy at the feed retailer
had a good humorousness. Protecting these eggs from
predators and nosy neighbors is the one other consideration.
Here is my web-site :: nancy pelosi
March 22nd, 2014 at 5:59 pm
Helpo there! Do yoou use Twitter? I'd like to follow you if that
woould be ok. I'm definitely enjoying your blog and look forward to
new posts.
Here is my page :: garcinia cambogia total lean side effects