LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   running a php script every minute (https://www.linuxquestions.org/questions/programming-9/running-a-php-script-every-minute-685702/)

navneet77 11-24-2008 05:19 AM

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

Bdfy 11-24-2008 05:30 AM

There is a better way, but if you want you may run php script in startup scripts as a daemon.

xhypno 11-24-2008 09:48 AM

Quote:

Originally Posted by Bdfy (Post 3352616)
There is a better way, but if you want you may run php script in startup scripts as a daemon.

Running as a daemon is not right for his needs. Cron would be the correct thing to use. The point of Daemons are to run as threaded services no the singular repeating of an action.

TB0ne 11-24-2008 10:31 AM

Quote:

Originally Posted by xhypno (Post 3352811)
Running as a daemon is not right for his needs. Cron would be the correct thing to use. The point of Daemons are to run as threaded services no the singular repeating of an action.

Cron would be ONE way of doing it, but is overkill. Rather than CRON firing off a job, and logging what it does, along with the job itself doing something, why not just put a "sleep(60)" in your script, and loop through it again?

xhypno 11-24-2008 10:35 AM

Quote:

Originally Posted by TB0ne (Post 3352844)
Cron would be ONE way of doing it, but is overkill. Rather than CRON firing off a job, and logging what it does, along with the job itself doing something, why not just put a "sleep(60)" in your script, and loop through it again?

Doing that would require the script to claim a copy of the interpreter in memory till the script is killed.

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.

TB0ne 11-24-2008 10:56 AM

Quote:

Originally Posted by xhypno (Post 3352847)
Doing that would require the script to claim a copy of the interpreter in memory till the script is killed.

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.

Good point, I forgot this was a PHP script he was talking about.

geek_man 11-24-2008 12:46 PM

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.

xhypno 11-24-2008 12:49 PM

Quote:

Originally Posted by geek_man (Post 3352986)
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.

Wouldn't matter what the script is for. If it is written already, there is no benifit to rewrite it in perl. As for it running, the OP stated that he needs it to run every minute so stating that you would have cron run it every 5 minutes doesn't help him much.

navneet77 11-24-2008 03:30 PM

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

jlinkels 11-24-2008 07:19 PM

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

xhypno 11-24-2008 10:08 PM

Quote:

Originally Posted by jlinkels (Post 3353353)
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


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

keefaz 11-25-2008 08:06 AM

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

nehaandrew 11-25-2008 12:41 PM

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

WebbDawg 11-25-2008 02:50 PM

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.

chrism01 11-25-2008 11:04 PM

@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.