Solved

Can a CRON job run a PHP script

Posted on 2010-09-14
7
820 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 83

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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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 110

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 110

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SSH in linux 9 71
Certificate Request CentOS/Apache 1 24
exchange, squid, proxy, linux 6 44
check the file dates in unix 14 31
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 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