user123
user123

Reputation: 5407

Executing script as daemon process in PHP

This script in my php page javascript which is called on page load:

<script type="text/javascript">
        var id =0;
        function getData(){    
        $.ajax({
              url       : "refresh.php",
              type      : "POST",
              data      : {"id" : id},            
              success   : function(data) {
                 $(".show").html(data);
              }
          });
    }
        $(document).ready(function(){
      setInterval("getData()",36000);//Polls in every x sec
    });
    </script>   

refresh.php does my actual process, which I want to get executed every 1 hour

What is the way so that refresh.php get executed autmatically at everyone hour?

refresh.php

<?php
    error_reporting(E_ERROR | E_PARSE);
    $feedUrl = "http://api.frrole.com/v1/curated-content?location=India&contenttype=link&orderby=popularity&apikey=wmqmz15svzH8qSy5yQuF52b068a40a69e";
    $json = file_get_contents($feedUrl);
    $code = json_decode($json,true);

    foreach($code as $arr)
    {
    foreach($arr as $k=>$v)
    {
    echo $arr[$k]['title']."<br>";
    echo $arr[$k]['city']."<br>";
    //inserts this vvalue in db
    echo "<br>";
    }
    }

?>

I have seen all result of execute php script in background om Google

which show solution like this:

  1. nohup php myscript.php &

    but nohup has some issues

  2. 2.

exec("/bin/bash -c '/usr/bin/php /path/to/child.php 2> /dev/null' &");

and other

I am confused. Which one is correct way to do this?

Upvotes: 0

Views: 87

Answers (2)

geekInside
geekInside

Reputation: 588

use crontab -e if you are on linux then enter this for every hour

0 * * * * /bin/php /your_path/refresh.php

then save the file

:wq

Upvotes: 1

Laird
Laird

Reputation: 138

If you want a script to execute automatically every hour, you might want to investigate cron. Most web hosts offer cron services through their control panel at least.

Upvotes: 1

Related Questions