LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-24-2008, 05:19 AM   #1
navneet77
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Rep: Reputation: 0
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
 
Old 11-24-2008, 05:30 AM   #2
Bdfy
LQ Newbie
 
Registered: Nov 2008
Posts: 12

Rep: Reputation: 0
There is a better way, but if you want you may run php script in startup scripts as a daemon.
 
Old 11-24-2008, 09:48 AM   #3
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by Bdfy View Post
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.
 
Old 11-24-2008, 10:31 AM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,600

Rep: Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570
Quote:
Originally Posted by xhypno View Post
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?
 
Old 11-24-2008, 10:35 AM   #5
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by TB0ne View Post
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.
 
Old 11-24-2008, 10:56 AM   #6
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,600

Rep: Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570Reputation: 2570
Quote:
Originally Posted by xhypno View Post
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.
 
Old 11-24-2008, 12:46 PM   #7
geek_man
Member
 
Registered: Nov 2007
Distribution: Fedora 7, Fedora 9, Solaris 10, Mac OS X, RHEL5
Posts: 71

Rep: Reputation: 15
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.
 
Old 11-24-2008, 12:49 PM   #8
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by geek_man View Post
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.
 
Old 11-24-2008, 03:30 PM   #9
navneet77
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Original Poster
Rep: Reputation: 0
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
 
Old 11-24-2008, 07:19 PM   #10
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,095

Rep: Reputation: 493Reputation: 493Reputation: 493Reputation: 493Reputation: 493
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
 
Old 11-24-2008, 10:08 PM   #11
xhypno
Member
 
Registered: Sep 2004
Posts: 62

Rep: Reputation: 16
Quote:
Originally Posted by jlinkels View Post
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
 
Old 11-25-2008, 08:06 AM   #12
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,559

Rep: Reputation: 121Reputation: 121
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
 
Old 11-25-2008, 12:41 PM   #13
nehaandrew
Member
 
Registered: Nov 2008
Posts: 53

Rep: Reputation: 15
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

Last edited by nehaandrew; 11-30-2008 at 01:14 AM.
 
Old 11-25-2008, 02:50 PM   #14
WebbDawg
LQ Newbie
 
Registered: Jun 2008
Posts: 1

Rep: Reputation: 0
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.
 
Old 11-25-2008, 11:04 PM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,269

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
@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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i get an error message running php script inside a cgi script. repolona Linux - Software 0 02-22-2007 09:10 PM
cron NOT running php script ??!! hendrixx Linux - Software 2 11-15-2006 07:18 AM
cron NOT running php script ??!! hendrixx Programming 1 01-19-2005 05:36 AM
php script not running in FC2 benjam Fedora 2 08-09-2004 02:47 PM
problems running php script rhuser Linux - Software 5 02-14-2003 01:01 PM


All times are GMT -5. The time now is 11:49 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration