running a php script every minute
Hi,
I need to download a datafeed another website (for ex. a forex feed), which will be used on this website. This need to be updated quite regularly. Therefore i was thinking of using cron to call a php script every minute Would this be the right approach or is there a better way Thanks for any help Navneet |
There is a better way, but if you want you may run php script in startup scripts as a daemon.
|
Quote:
|
Quote:
|
Quote:
This causes 2 problems, the standard timeout is 60 seconds for a script in the php.ini and the second is every time a php script runs it opens another copy of the interpreter and the interpreter is more memory intensive to keep in memory then to fire it off each time the cron runs. |
Quote:
|
Do you need the script to be in PHP? Can you port your script to Perl? I guess this would be better.
Do you need the script to be run every minute? Is it really necessary? I would execute it every 5 minutes with cron. |
Quote:
|
thanks for all the help. This is a great forum, it was my first post here.
Well as for perl i already have it in php and not very good in perl. Running it 5 minutes wont be an option as the data is time sensitive and the earlier the more valuable. it seems cron should do for now regards |
My rule of thumb is that delays shorter than 2 minutes should be implemented using sleep. CRON is not quite accurate with this short intervals. If a process has to run on a minute, what could be the jitter?
As for memory use, it doesn't matter THAT much. The footprint of the PHP interpreter is not very large. And NOT having in memory doesn't bring much. If the memory which is used by PHP is needed by another process, swapping might occur while PHP sleeps, but that is dependent on swap algorthm being in effect. If you use CRON and there is not sufficient memory the system starts to swap as well, but then for sure. If you have enough memory to accomodate PHP without swapping, there is no need to run and terminate every time. If you run multiple PHP daemons, memory usage per process is decreased as libraries are shared among processes. I am currently running a PHP daemon. User allocated memory is 1.5 MB. Reported memory use is 2%, the system being equipped with 128 MB RAM. jlinkels |
Quote:
As of PHP 5 library sharing is not part of the core of PHP unless patched and compiled in. The standard php build without extra modules added in uses 6-10mb of ram without loadable modules via the ini. On my production boxes with pgsql, mysql, sqsh, ldap, xml, soap, and zlib loaded via the ini avgs 23mb with no scripts active. This server is hit a lot and at almost any time sees 3.5-4GB of ram being used to running instances of PHP. So trust me, even though cron is not that accurate for timing, using sleep in a production environment to keep an instance of the interpreter running is not practical. Xhypno |
You could also download your data with a client request, then store it with a time stamp on a cache file or database, then when another request is done, you check the time stamp with the time of the new request, then you act accordingly (download new data or not)
This has the advantage that it saves some bandwidth, if there is no data request for a long time, but I don't know.. just an idea |
The two options I would suggest are:
1. Run a cron job to run once in every "X" seconds ("X" being the time gap you want) 2. Run a script that does all the requisite processing - and then sleeps for "X" seconds ("X" being the time gap you want) Linux |
I use cron to execute a php script every 30 minutes for ad reshuffling (re-write html for a .inc page) and it works great. It keeps the advertisers happy and all the ads are always displayed on the page just reshuffled every 30 mnutes.
cron works great for this. |
@nehaandrew: the smallest increment in cron is 1 minute....
Personally, I take it as a rule of thumb that anything more frequent than every 5 mins and I write a daemon that waits the requisite amt of time at the bottom of a loop. There's a system overhead in creating new processes every minute. In some cases you also have to worry about processes running into each other ie proc 1 not complete when proc 2 starts etc. |
All times are GMT -5. The time now is 04:35 PM. |