Solved

Can a CRON job run a PHP script

Posted on 2010-09-14
7
799 Views
Last Modified: 2012-05-10
Dear Experts,

I am wanting to take full advantage of CRON on my server as there are a lot of repetitive task I do every day that could easy be set up to do automatically. But I have not used CRON before and I have read that it can run a PHP script.

If I created a PHP script and set the trigger to be "on page load" would this be able to be run by CRON at 24 hour intervals. I am also planning to set up a trigger on the PHP script to send me an email to let me know what has been changed so I can keep track. Are there any restrictions or things I should be aware of in doing this.

Many thanks as always to the experts community. I must say that I have worked in many different areas from film, photography, design and a few others and know I am learn a little about programming and web development and PHP as well as web design and I am just amazed everyday and what a truly lovely group of human being developers and programs are. It really lovely to be a part of it. Thank you.
0
Comment
Question by:Lightwalker
7 Comments
 
LVL 15

Assisted Solution

by:ludofulop
ludofulop earned 50 total points
ID: 33669746
hi, you can insert to your cron something like this :

0 1 * * * cd /var/www/ && /usr/local/bin/php yourscript.php 1> /dev/null

this will run yourscript.php each day at 01:00
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 33669750
PHP CRON jobs need to be command line PHP programs so they can run under your user.  I don't know if CRON can run as the web user since you don't have permission to be that user if you're not coming in thru the web server.  Other than that, I think it can do what 'you' can do with your files.
0
 
LVL 16

Expert Comment

by:santoshmotwani
ID: 33669790
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 13

Expert Comment

by:darren-w-
ID: 33669791
Joomla sets up a cron job to run a php script, example here: http://joomlapanel.com/joomla-article/joomla-tutorial/258-setup-cron-job-for-your-joomla-site.html

This page gives some instructions on how to do it.
0
 

Assisted Solution

by:mallaravi
mallaravi earned 50 total points
ID: 33670038
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 150 total points
ID: 33670731
Yes you can run PHP scripts as cron jobs.  Your hosting company will undoubtedly have instructions that tell you how to set up the triggers and time intervals so that the cron jobs run at the expected times.

Here is a "gotcha" (works as designed, just not as expected) that DaveBaldwin alluded to above: Many of the PHP variables are different when you are running a cron job.  For example, $_SERVER["DOCUMENT_ROOT"] will almost certainly not point to your directories, and the SESSION concept is useless.  I recommend that your first cron job tests include this little script which is untested, but valid in principle.

Best regards, ~Ray
<?php // EXPLORE CRON

error_reporting(E_ALL);

ob_start();

phpinfo();

$info= = ob_get_clean();

mail('You@Your.org', 'CRON INFO', $info);

Open in new window

0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 150 total points
ID: 33670778
Here is the tested version.  It seems useful, so I think I will keep a copy in my teaching library.
<?php // RAY_cron_job.php
error_reporting(E_ALL);


// THIS SCRIPT CAN BE RUN AS A REGULAR WEB PAGE AND AS A CRON JOB
// IT SENDS AN EMAIL -- INSERT YOUR EMAIL ADDRESS IN THE LAST LINE
// YOU WANT TO COPY THE EMAIL YOU GET INTO HTML FILES ON YOUR SERVER
// AND LOOK AT THE DIFFERENCES BETWEEN THE TWO PHP ENVIRONMENTS


// START THE OUTPUT BUFFER TO CAPTURE THE STDOUT STRING
ob_start();

// GET THE PHP ENVIRONMENTAL DATA
phpinfo();

// COPY THE OUTPUT BUFFER
$info = ob_get_clean();

// EMAIL IT TO YOURSELF - SET YOUR OWN EMAIL ADDRESS HERE
mail('You@Your.org', 'CRON INFO', $info);

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now