How to Execute PHP Code Periodically in an Automatic Way

How can I make my PHP script run at a certain time everyday?

What you want is called a cron job and is already covered here:

PHP: running scheduled jobs (cron jobs)

Schedule and execute a PHP script automatically

You'll need to add a scheduled task to call the URL.

First of all, read up here:
MS KB - this is for Windows XP.

Second, you'll need some way to call the URL - i'd recommend using something like wget - this way you can call the URL and save the output to a file, so you can see what the debug output is. You can get hold of wget on this page.

Final step is, as Gabriel says, write a batch file to tie all this up, then away you go.

e: wget is pretty simple to use, but if you have any issues, leave a comment and I'll help out.

ee: thinking about it, you don't even really need a batch file, and could just call wget directly..

PHP script to execute at certain times

The idea of cron and scheudled jobs seems to run counter to what you're actually trying to do. If you want something to display (an iframe in this case) only during certain times, you can simply check the server time during each request, and opt to display it if you're within a given time period.

Something like this will produce the same effect as a cron job, with more granularity, checking the time at the exact moment the requst is made.

<!-- Your Header here -->

<?php
$hour = date('G'); // 0 .. 23

// Show our iframe between 9am and 5pm
if ($hour >= 9 && $hour <= 17) { ?>
<iframe .... ></iframe>
<?php } ?>

You can expand on the conditional statement to show the iframe multiple times per day, or have your script check whatever external condition you're looking to use to govern the display of your iframe.

Update:
Additional times or types of comparisons could be specified via something like

<?php 
$hour = date('G');
$day = date('N'); // 1..7 for Monday to Sunday

if (($hour >= 5 && $hour <= 7) // 5am - 7am
|| ($hour >= 10 && $hour <= 12) // 10am - 12 noon
|| ($hour >= 15 && $hour <= 19) // 3pm - 7pm
|| ($day == 5) // Friday
) { ?>
<iframe...></iframe>
<?php } ?>

The idea of periodically adding/removing the iframe from below your header with a server-side cron/task scheduler job is far more complex than simply conditionally displaying it during each request.

Even if you have some specific task which must run, such as a periodically generated report, the actual job of displaying the results usually don't fall upon the periodic task. The PHP script responsible for showing that iframe would still query the database at the time the request is made for any new content to show, and display it if found, rather than the periodic task somehow modifying the script to include an iframe.

run php script automatically

Two options:

  1. Use crontab demon
  2. Hire a worker and make him open script in a browser every 24 hours

The choice is yours :)

To use crontab, type crontab -e in console, the text file opens. Add a line at the end:

0 0 * * * /usr/bin/php /var/www/mysite/httpdocs/daily_stats.php

Where:

0 0 * * * - run every day at 00:00

/usr/bin/php -path to your PHP (can be determined by which php command)

/var/www/mysite/httpdocs/daily_stats.php - path to your PHP script

if which php outputs nothing, install PHP cli by running:

sudo aptitude install php5-cli 

Good luck!

How can I run PHP script in certain interval (e.g. once a day)?

If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.

Automatically execute a PHP script

Using the following:

  • Use cron to automate the task (more info)
  • If you wish to have the page as a web page you'll want to use wget to load the web page (more info)
  • Alternatively, use PHP from the command line by doing something like /path/to/php /path/to/script.php - you need to make sure you give this file (script.php) executable permissions. (more info)

Edit: You can use cronjobs on a shared server, what control panel is available to you? (cPanel, Plesk, etc.)



Related Topics



Leave a reply



Submit