I have a php function live on my webpage. It runs whenever someone reloads that page and probably when google etc. crawls it. I only want the major part of it to execute once every hour though.
I was thinking about storing a timestamp in a database and only execute it if 59 minutes has passed since it ran last. Is there a better/simpler way? Could it for instance be stored in some global lasting php variable or such?
It seems a bit overkill to create a table and only keep one timestamp in it. I don't have access to the local machine the code is stored on.
Thanks for any suggestion!
As AmazingDreams said, using a cron job is probably the best approach for this, especially seeing as it looks as if you're wanting to run this script once every hour. If you're using Windows, you can use the Windows Task Scheduler instead.
Related
I have been researching on how to approach this. What I am trying to prevent is an overlapping execution of a cronjob. I would like to run my script in every minute basis because the application is support needs a constant look out. The problem is if it takes quite a long time to finish and the next cron execute will catch up.
I have searched and some posted about PID but did not get on how to do it. I cannot use lock files because it can be unreliable, tried it already.
Is there any other approach on this?
Thank you.
Get each job to write to a database in completion. Then put an if statement at the start of each script to ensure that the other script has run and completed (by checking your database).
Alternatively...
You could have your first script run your second script at the end?
I set up a very small (internal) dedicated web server, and I need to pull some energy data every 10 seconds or so from an XML file. This is the PHP code I have thus far:
<?php
$mydata = simplexml_load_file('http://192.168.x.xx:yyy/data.xml');
echo $mydata->device[0]->name;
echo $mydata->device[0]->value;
?>
I tested similar code out on my web server and PHP is installed and I think this should work, but I'd like to have this run every 10 seconds or so. This way the data displaying on my web page is always up to date. The web page will be left running 24/7 as a sign on the wall. What's the easiest way to refresh the data?
I would simply refresh the portion of the web page that displays the data using Ajax. Trigger the refresh using a JavaScript timer.
The easiest way? Add this line to your page.
<meta http-equiv="refresh" content="10">
This may not be the best way though, especially if you have a lot of stuff on the page that you don't need reloaded every 10 seconds. If that's the case, you should look into AJAX.
If you have a page setup that returns only the data you need (like your example) you can make an asynchronous request every 10 seconds using JavaScript's setInterval() to get the latest data and show it.
If you are running this through a browser then I would go with Eric's answer.
However if you have this running from command line you can do one of the two:
Have your current script say pull_energy_data.php to run from a cron job every 10 seconds. Don't forget to create some sort of locking mechanism. Just in case the job takes more than 10 seconds to run and you'll have more than one script running at the same time.
Another approach is having a script wrapping your pull_energy_data.php running in a loop and executing it every 10 seconds. This is less desirable than the previous approach as you'll need to keep track of when pull_energy_data.php last ran and how to issue a command to stop the wrapper script.
assuming you are running the server in Linux/Unix, perhaps look into writing a cronjob (automated job) for it?
One solution I can think of is to run the PHP script as a cronjob every 10 seconds, writing the output into a file or a database table.
You can then write a separate PHP script that reads the contents of the file/DB entry whenever anyone loads it in a browser.
is there any other option other than cron to schedule the running of a php backup script at a certain time?. I know you can use php itself to schedule things, but it will only fire if the site is getting traffic.
Are there any other options ?.
Thanks :-)
If you're talking about a database backup, then MySQL 5.1 and above has CREATE EVENT which can be used to trigger events (such as stored procedures that can dump table structure/data to file) at regular intervals or set times
Well, cron jobs is a solution. But not necessary in most cases.
If your script is doing something off the site (like sending an email or something), it must be a cron-job.
But...
I made a textbased rpg-game once where several actions were stored in the database waiting to get triggered at a specified time. I found out that it did not make any difference if the script fired at the time it should, or when the first person visiting the page after the time is beyond the timestamp. You could do these events before displaying the content of the page. (I used a file called monitor, to keep it simple).
Would you like to say more about your "event"?
Unless you feel like writing a daemon/service/etc., cron would be your best bet. If you need a job ran more often than minutely, use a lockfile solution and a looping script.
Well not really, Crons are your best bet.
Other than that call a script, and if certain parameters are met such as time elapsed then run the script.
I am looking to make a script that runs and uses the time stamp of the last time it ran as a parameter to retrieve results that have been updated since that time. We were thinking of creating a database table and having it update that and then retrieve the date from there, but I was looking for any other approach that someone might suggest.
Using a database table to store the last run time is probably the easiest approach, especially if you already have that infrastructure in place. A nice thing about this method is that you can write the run time right before the script terminates, in case it runs for a long time and you do not want it to start up again too soon.
Alternatively you could either write a timestamp to file (which has it's own set of issues) or attempt to fish it out of a log file (for example, the web access log if the script is being run that way) but both of those seem harder.
This might work: http://us.php.net/manual/en/function.fileatime.php (pass it $_SERVER['SCRIPT_FILENAME'])
Your best result would be to store your last run time. You could do this in a database if you need historical information, or you can just have a file that stores it.
Depending on how you run the script, you may be able to see it in your logs, but storing it yourself will be easier.
Say I have a game in PHP which is a money making game and everyday the prices on items change on the market.
How would I make it so the prices in the database are automatically updated each day? Would I install a program on the server that detects when it has gone 24 hours then runs a PHP file which does the updating? Or os there another way?
Edit:
The thing is guys, I don't actually own the server I rent it from a hosting company so I don't really have access the the command line :s
Thanks, Stanni
Assuming you're on a Unix system, you should setup a daily cronjob. To do this, run "crontab -e" and enter something like:
9 21 * * * /path/to/your/script
This will run at 21:09 every day.
Since you probably don't have access to cron either what I would do is check how much time has passed everytime someone loads a page. If 24 hours have passed then call your update function. If 48 hours have passed then call it twice. If no one loads the page then it doesn't matter if the update function has been called or not because no one is looking ;)
Or you could setup a computer at home to call your update.php remotely every 24 hours. You can do that with a cron job and wget or if you're using windows you could use the task scheduler.
I think the first option will work the best. Call your update function every page load and only update when the 24 hour mark has passed. If you write it correctly it doesn't matter if it gets updated at the exact 24 hour mark.
If you don't have access to the commandline, you could add a 1x1 image to the website which calls a php script which checks if there needs something be updated.
something like
<img style="width: 1px; height: 1px; visibility: hidden" src="cron.php">
In cron.php you check if the data needs to be updated.
You want to set up a "cron job", or a PHP file that runs at a certain interval you set.
Check out this article for more information.
The best part about cron jobs is that you are not limited to the small subset of functionality available in say, stored procedures. You can use whatever logic you like! :)
Use webcron :)
http://www.webcron.org/index.php?lang=en
Or here is a good list:
http://www.onlinecronservices.com/
If you have a script that updates the prices and all you want to do is run it every day, use Cron (linux) or at command (windows).
How could you not 'have access to the command line'? I can't think of any host that doesn't allow ssh access. Having access to cron is a different story, but they SHOULD allow this, also. If they don't - find a new host!
Forget all that. There is no reason to do anything like that.
All you have to do is check each time someone calls the webpage. You keep track of the date and when the current date no longer matches your variable then you fire off the thing that gets new data. Bam! Of course that's not very scalable but it's fine for a small game.
I think that you can make a function that gets called every time a page is accessed, and verifies if the update took place, checking against a database.
So in a quick pseudo code:
function verify_often(){
if (last_update_in_db() != today() ){
update_db();
run_periodic_function();
}
return 0;
}
This method requires only the classic PHP & MySQL combination.
I couldn't work out how to reply to alex's answer but I wish to mention something that they said. When they said "check how much time has passed everytime someone loads a page" I feel that you don't need to check it every time the page is loaded. The better way of doing it would be to check when a user logs in. If it goes over 24 hours while users are still logged in it will not matter for the described scenario. The next time someone logs in the prices of items will change.