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
Solved

Can a CRON job run a PHP script

Posted on 2010-09-14
7
814 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Creating and Managing Databases with phpMyAdmin in cPanel.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

809 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