?
Solved

Can a CRON job run a PHP script

Posted on 2010-09-14
7
Medium Priority
?
835 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
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
Suggested Courses

765 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