[php] Reloading php file outside of browser

I need to be able to have a PHP file that loads itself outside of the browser.  I want to avoid using a cron tab if I can, but I am not sure how to do this with php, to have it run in an interval on the server every second or faster.  

The script is just going to be loading a JSON file and checking for changes to it and then uploading any changes to it to a database.

Thanks!
prileyosborneAsked:
Who is Participating?
 
theodorejsalvoCommented:
You can run php scripts from the command line if you have php installed.  For example, in linux:

$
php myscript.php

Open in new window


Any print statements will go to standard output instead of to a browser.

Also, this would be easy to implement to setup in cron by (assuming running once a second):

*/60 * * * * php myscript.php > /var/log/myscript.php.log 2>&1

Open in new window


The other option to avoid cron would be to create an engine in your script and have the script repeat itself.  It would still need to be started to kick off, but you could do this also via startup scripts, etc.
0
 
InsoftserviceCommented:
try out this one

refresh ur page with jquery.

<script type="text/javascript">

$(document).ready(function(){
   var j = jQuery.noConflict();
      j(document).ready(function()
      {
            j(".refreshMe").everyTime(5000,function(i){
                  j.ajax({
                    url: "refresh-me.php",
                    cache: false,
                    success: function(html){
                        j(".refreshMe").html(html);
                    }
                  })
            })
      });
   j('.refreshMe').css({color:"red"});
});



</script>
0
 
prileyosborneAuthor Commented:
insoftservice: Does this require me having my browser open to activate the refresh?  If not, can you kind of explain the function a bit so I understand how it works in case I need to adjust it a bit.

theodorejsalvo::
For that cron to work, how would I implement it?  It would have to be in a separate file on the server I assume, but how do I set it to run. This server doesn't use cpanel and I only have access to a single directory on it.

As for an engine and start up script, do you happen to have a link to any examples of these or any PHP functions that might be useful to accomplish these?  Thanks so much!

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
prileyosborneAuthor Commented:
Also, for a cron tab, if I have a file in my etc directory called crontab and it already has some cron calls, can I add this to that file and not ruin the other calls?

Thanks, sorry, total cron noob.
0
 
theodorejsalvoCommented:
Yes you can...and if you can, that is the easiest way.

If you don't have shell access, cron won't work.

Further questions:
Where is the JSON file that you are loading and checking, on the server?
What database is it sending changes to, the database on the server?
For file/database access, does the PHP script need to run on the server, or can you run it from an external machine?  In other words, can you access the file and database externally?
0
 
prileyosborneAuthor Commented:
Thanks for the quick feedback!  Here are the answers to your questions:

Where is the JSON file that you are loading and checking, on the server?
It is on the same server but using a different port.  (so 01.01.01.0101:1010 for example)

What database is it sending changes to, the database on the server?
Yes, it is a database hosted on the server. I am using the server ip to access it since the actual server it resides on has a lot of different partitions and frameworks on it.


For file/database access, does the PHP script need to run on the server, or can you run it from an external machine?  In other words, can you access the file and database externally?

I could probably run it locally and tunnel in using Putty or something, but I would prefer it resides on the server if possible.

Thank you so much and let me know if you need anything else!

0
 
InsoftserviceCommented:
did u tried my script.
just replace it with ur php page it will help u
0
 
prileyosborneAuthor Commented:
insoftservice,  will that script work outside of a browser?  I need to refresh this page even when I am not browsing to it. Thanks!

0
 
prileyosborneAuthor Commented:
For those keeping score, I had to use the crontab to refresh the script. I also was able to secure command line access wit putty through SSH, so I am able to use it.  So any suggestions would be great, but if not, I will probably close this topic since I was able to get the file to load every minute with a crontoab.

Thanks again!
0
 
InsoftserviceCommented:
yes.
0
 
prileyosborneAuthor Commented:
After I got command line access, this is how I set up my cron and am beginning to set up a daemon to run the file more often than every minute. Thanks for all the advice from everyone!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.