Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Can a CRON job run a PHP script

Posted on 2010-09-14
7
Medium Priority
?
846 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 15

Assisted Solution

by:ludofulop
ludofulop earned 200 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 84

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 total points
ID: 33670038
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 600 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 111

Accepted Solution

by:
Ray Paseur earned 600 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

598 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