bash - Executing script as daemon process in PHP

231

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 thatrefresh.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 ofexecute 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?

510

Answer

Solution:

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.

840

Answer

Solution:

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

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

then save the file

:wq

People are also looking for solutions to the problem: php - To load controller using codeigniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.