ok, now i have 3 files, which is update_XML.php, showXML.html and data.xml.
i use the update_XML.php to update the data in data.xml, then i use showXML.html to display the data.
my question is how should i call the update_xml.php to update the data.xml automtically?
i only manage to do it manually.
what already in my mind is only use timeout() in html to refresh the showXML.html
but how about the updating part ? use timeout() function too?
i need some advice about this, especially a correct flow.
thx
Depending on if your website is accessible to just you or not, if it's not update_XML.php will be called for every user when using setTimeout();, also JS can be disabled.
Your option is setting up a cron job on your server.
cron jobs are basicly automated tasks on a scheduled base depending on how often you want this cron job to be executed.
Here is some documentation on cronjobs
NOTE this is a tutorial for *nix systems.
Related
I want to ask is there a way to track database table using php mysql.
I want to do something like i have a table called post. Now when user post some data other user need to view this data. That is latest one need to be view to user on the top. We can do this by refreshing div after every few sec or using ajax. But can we use Trigger. As we know it automatically fires when something is executed. Hence i want to know can we use trigger in PHP code to automatically detect changes in table. And when a new post is available it needs to return the data from database. Please give me a brief description about this. Thank you in advance.
The trigger is executed on Mysql Server, not on the PHP one (even if those are both on the same machine).
So, I would say this is not quite possible -- at least not simply.
Still, considering this entry from the MySQL FAQ on Triggers :
23.5.11: Can triggers call an external application through a UDF?
Yes. For example, a trigger could invoke the sys_exec() UDF available at MySQL Forge here: http://forge.mysql.com/projects/project.php?id=211
So, there might be a waty, actually, via an UDF function that would launch the php executable/script ; not that easy, but seems possible ;-)
Read more about it in:
https://stackoverflow.com/a/1467387/3653989
SQL trigger is a database object executed server-side.
You want a front-end technique to refresh your data without refreshing the whole page.
You can refresh your page using:
<meta http-equiv="refresh" content="5">
With PHP, you can refresh the page using:
header("refresh: 3;");
but no-one would suggest you to use such a method, because your need is refreshing the page, only after a change in your database, and not continuously.
So, if you already use PHP, you need Javascript Push technology:
Push, or server push, describes a style of Internet-based communication where the request for a given transaction is initiated by the publisher or central server. (wikipedia)
JavaScript polling, long-polling, real-time techniques, and javascript frameworks such as jquery, node.js, socket.io include a lot of practices that give you this possibility.
I'm pretty new to PHP. This is my first time actually, so I apologize in advance if the question sounds dumb.
I have a php script which fetches data from an external API and updates my database regularly. I know that the best way to do this is to use a cron job. However, I am using an infinite loop which sleeps for a particular time between each update.
Now, I need to allow the user (admin) to start / stop the script and also allow them to change the time interval between each update. The admin does this through a UI. What is the best way to do this? How do I execute the script in the background when the user clicks start and how do I stop the script?
Thanks.
I think the ideal solution would be the following:
Have the background job run as a cronjob every minute (instead of a loop which can cause memory leaks). PHP was not designed to be a daemon.
Set a DB flag in the cronjob for on/off, everytime it runs it checks if its on or off, if off it exists if on it continue.
In the UI you turn that flag on or off depending on what the admin needs.
I think that is the best way to go (and easiest).
You might want to take a look at Threading in PHP -> http://www.php.net/manual/en/class.thread.php
An other option is to either set a SESSION (which is a little tricky, since you won't catch it in the same call) or you generate a file wich tells the script to stop/exit/fail/what ever if that file exists.
I got some long running bash scripts on my Ubuntu 10.04 Server that are doing things like backups and database dumps.
I'm currently executing them via cron and mailing me the results. However it would be nice to trigger them manually throughout a Webinterface additionally. (I do not want to give any user SSH access so he can trigger the scripts)
In PHP, for instance, there is the exec Command which obtains the Script output AFTER the Script has finished. This is not useful since the Scripts last longer than the 30 Seconds Browser Timeout. (And I want to monitor the progress) So I'm currently stuck in finding a way on how to gather the Script output periodically or "tailing" and display it through JavaScript or Pagereloads or something like that.
As I noticed, Webmin for instance is somehow doing this when you run system commands like apt through their webinterface.
One way I could imagine is forking the script execution and redirecting the output to a file, whilst the Main Process is reading it on Page reload/ Ajax request. But this sounds somehow like a workaround :)
So, does anyone have a good solution for this problem? Doesn't need to be PHP, could also be python or perl or anything else on server side. Or is there a tool or library for such things?
Any ideas greatly appreciated!
I am not sure if you mean to require PHP in your solution, but if not…
In the past, I have used Jenkins for this kind of thing. You can use it as both a cron replacement and as a way to manually trigger scripts, all while keeping logs of what happens.
Jenkins can be set up such that some users only have permission to trigger jobs, as opposed to the Jenkins administrator who can do stuff like set up and edit jobs, etc.
And if you like getting email, Jenkins can do that too. If anything, the biggest downside to Jenkins is that it is super-flexible / configurable, and so not always obvious how best to set it up for one's particular needs.
Any language would work. I can recommend the sledgehammer approach which would be Webmin.
http://www.webmin.com/
the advantage is what you asked for is baked in and you wouldnt have to program anything.
in php you should look at
buffers and flushing. You can basically output to the page as you recieve output.
http://php.net/manual/en/function.ob-start.php
see this related question for the popen sytax.
php shell_exec with realtime updating
I did something similar ( quick and dirty but might not be very efficient )
I used a database log table to store the output ( from the back end script )
The webpage would then make periodic ajax requests to get only those logs that it has not fetched previously and display(append) it to the front end.
The database table was very simple with columns
id(auto increment )
log_text
script_identifier ( unique if you are tracking multiple scripts )
last_modified_time
output_displayed ( value = 0 tells that this output has not been read by the ajax )
A simple process would be like this
Open a webpage and click a button to send an ajax request to start the back end script
At every 5 seconds send another ajax request to see if there are unread log table rows
If there are unread rows, mark them as read, fetch them and display
You can improve in many things like
use apc or memcached instead of database table
keep the log table small or dynamically create log tables for each script
How can I make a scheduler in PHP without writing a cron script? Is there any standard solution?
Feature [For example]: sent remainder to all subscriber 24hrs b4 the subscription expires.
The standard solution is to use cron on Unix-like operating systems and Scheduled Tasks on Windows.
If you don't want to use cron, I suppose you could try to rig something up using at. But it is difficult to imagine a situation where cron is a problem but at is A-OK.
The solution I see is a loop (for or while) and a sleep(3600*24);
Execute it through a sending ajax call every set interval of yours through javascript
Please read my final opinion at the bottom before rushing to implement.
Cron really is the best way to schedule things. It's simple, effective and widely available.
Having said that, if cron is not available or you absolutely don't want to use it, two general approaches for a non-cron, Apache/PHP pseudo cron running on a traditional web server, is as follows.
Check using a loadable resource
Embed an image/script/stylesheet/other somewhere on each web page. Images are probably the best supported by browsers (if javascript is turned off there's no guarantee that the browser will even load .js source files). This page will send headers and empty data back to the browser (a 1x1 clear .gif is fine - look at fpassthru)
from the php manual notes
<?php
header("Content-Length: 0");
header("Connection: close");
flush();
// browser should be disconnected at this point
// and you can do your "cron" work here
?>
Check on each page load
For each task you want to automate, you would create some sort of callable API - static OOP, function calls - whatever. On each request you check to see if there is any work to do for a given task. This is similar to the above except you don't use a separate URL for the script. This could mean that the page takes a long time to load while the work is being performed.
This would involve a select query to your database on either a task table that records the last time a task has run, or simply directly on the data in question, in your example, perhaps on a subscription table.
Final opinion
You really shouldn't reinvent the wheel on this if possible. Cron is very easy to set up.
However, even if you decide that, in your opinion, cron is not easy to set up, consider this: for each and every page load on your site, you will be incurring the overhead of checking to see what needs to be done. True cron, on the other hand, will execute command line PHP on the schedule you set up (hourly, etc) which means your server is running the task checking code much less frequently.
Biggest potential problem without true cron
You run the risk of not having enough traffic to your site to actually get updates happening frequently enough.
Create a table of cronjob. In which keep the dates of cron job. Keep a condition, if today date is equal to the date in the creonjob table. then call for a method to execute. This works fine like CRON job.
I need to do some additional processing after a Drupal page has been sent.
I know I could fire a background shell command, but I need the current Drupal execution context to be maintained.
I've spent a lot of time looking, but I can't find any documentation in this regard. This is surprising because it must surely be a common requirement.
The only real idea I have is to fire up Drupal (again) via a shell command (exec, etc) and supply it with a pseudo path which would invoke the continued processing. But this seems unnecessarily complex/wasteful.
Any pointers greatly appreciated, tks.
UPDATE: Based on Googletorp and Matt's replies, I just want to point out that I'm not doing housekeeping with this additional processing. Without going into too much detail, I have a number of pages whose content is based on multiple nodes. If one of these child nodes changes then the page needs to be updated immediately, but there is no reason why the user who updated the child node needs to be kept waiting while this happens.
So, the control flow would be:
UPDATE CHILD NODE
RETURN UPDATED CHILD NODE VIEW TO USER (this is where Drupal would normally terminate)
REGENERATE PARENT PAGE
EXIT
Neither Cron nor the rules module have the immediacy I require - but thanks for your input.
This is something that you should be doing using http://drupal.org/project/job_queue, and cron to complete whatever you need. However if you want a quick fix and just want the page to appear and things to continue happening you can use the PHP function flush()
http://php.net/manual/en/function.flush.php
For background processes I recommend a cron, which is built into the drupal API with hook_cron.
Your update indicates you require immediate execution. Therefore you require forking or an asynchronous process. One option is pcntl_fork(), but I've had issues because (with mod_php at least) you don't really want to fork the web server process.
Your best option is probably to set up a page to specifically perform your update, then call it via a forked curl process through popen(). For example,
popen('curl http://localhost/update_parent_nodes &');
You can take a look at the rules module, which lets you run code whenever certain events happen.
Take a look at hook_exit() - it gets invoked from within drupal_page_footer(), right after the main content has been sent out to the client (which happens indirectly via the page_set_cache() call).
Be aware that it will usually not be invoked for JavaScript/AHAH callbacks, as those terminate the processing themselves earlier on in most cases, but for your described scenario, it should be the right way to go.
OP here. Just to share my solution with people for future reference:
I used the second Drupal shell template outlined here
This bootstraps Drupal from the command line. I modified it a bit so I could could specify my own function and args from the CL.
I then invoked the above script using the PHP system() call in conjunction with the background operator (&) - remember to redirect output or the & is irrelevant.
It's a bit messy, but it does exactly what I need.