Run a PHP script once every minute

10,774

Solution 1

You can run PHP code from the command line. e.g., if your PHP folder is in PATH:

php.exe C:\mycode\myfile.php

You can then set this up as a scheduled task in windows. Side note: be aware that certain things don't exist (and something exist in their place), e.g. Apache or IIS objects, as well as the full range of HTTP stuff.

Solution 2

Set up a cron job.

Solution 3

If you don't want to use cron; you could write a script to call it at the top of the minute

#!/bin/bash
while [ true ]; do 
  if [ $(expr $(date +%s) % 60) -eq 0 ]; then 
    echo "top o da minute";
    #put php script here
  fi; 
  sleep 1; 
done

Advantage/Disadvantage is that you will only spawn one copy of the script if it takes longer than a minute to complete.

Solution 4

<meta http-equiv="refresh" content="60" />

A very simple solution would be to add this html tag to your page. The page will reload every content seconds, and of course, execute again the php code.

Share:
10,774

Related videos on Youtube

Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I need to execute my PHP code every minute. Is there any way to do that?

    • markus
      markus over 14 years
      voting to close: x-duplicate and belongs on serverfault
  • Sampson
    Sampson over 14 years
    If the OP is new to PHP, he should be aware that Cron can have a bit of a learning curve.
  • Fluffy
    Fluffy over 14 years
    Shibin Moideen, there are some cron implementations for windows (plus the built-in task scheduler), I'd recommend nncron lite or sinc (sinc is not cron (this is the name))
  • aehiilrs
    aehiilrs over 14 years
    @roddik - Recursive acronyms are irritating. Gah.