jvalarta Posted August 15, 2006 Share Posted August 15, 2006 I need a script that runs 24/7/365 and constantly pulls data from one database and processses that information, then puts it into other databases.I can write the PHP no problem, my question is, can I do this with PHP? --Start it from the command line and will it run without stopping? (I would build a loop of course)Will this eat up memory? Processor power? Anyone ever done anything like this, and if so, any tips/links/examples? Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2006 Share Posted August 15, 2006 Just put the program into an infinant loop....[code=php:0]$a = TRUE;while ($a) { // do your database lookups in here // you might also want to use the sleep function sleep(5);}[/code]Yes it will use processing power. Will it kill your machine? No, it should be fine. Assuming your using Linux, use the [i]kill[/i] command to end the process. Id'e even write a bash wrapper around it so it can be killed easily. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 15, 2006 Share Posted August 15, 2006 Best setting a cron job (thast what they are for!).I have done so in the past - I think it is better to NOT loop but to let the app run once every minute (unless you are really desperate). Quote Link to comment Share on other sites More sharing options...
jvalarta Posted August 15, 2006 Author Share Posted August 15, 2006 A cron won't work becuase I need this to be running all the time, processing data as it comes in.I did build an enless loop last night, put it on a sandbox server, and let it run all night. Seemed to run fine, but all it did was write numbers to a flatfile.The other issue I ran into is that I have to keep a terminal window open. The second I close the window, the script stops.Suggestions on how to get around that?! Quote Link to comment Share on other sites More sharing options...
trq Posted August 15, 2006 Share Posted August 15, 2006 [quote]The second I close the window, the script stops.[/quote]Run it in the background. eg;[code]path/to/your/script.php &[/code] Quote Link to comment Share on other sites More sharing options...
jvalarta Posted August 15, 2006 Author Share Posted August 15, 2006 ??? That's it! sometimes the obvious is so hard to find. Thank you thorpe!So this leads me to another question, very related then...if you pass variables to a PHP script, how can you have that script release the browser and have the script continue to run and process?Like, if I wanted to kick off a script via the Web, and do this same thing? Quote Link to comment Share on other sites More sharing options...
trq Posted August 16, 2006 Share Posted August 16, 2006 [quote]Like, if I wanted to kick off a script via the Web, and do this same thing?[/quote]It really might be best if you explained exactly what your trying to do, and why you think it needs to be done this way. You really do NOT wan't to be trying to run a deamon from within another servers process. eg; If you really want to pass options to this deamon via http, you would need to build a http server into the deamon itself.Your really starting to get into quite a complex area. Its all possible... but not something your going to want to attempt without a real need. Quote Link to comment Share on other sites More sharing options...
jvalarta Posted August 16, 2006 Author Share Posted August 16, 2006 Believe me, I have a real need. This performance issue is really causing issues for our sales people. I must resolve this.So, we get data from our clients in a GET string (var1=foo&var2=bar, etc) and we pass it with an IMG tag (dont ask, its legacy and to switch to SOAP or something else would be a major change for our cusotmers) so when this IMG tag loads with the filename.php?getstring it holds up the browser page until the PHP is done running. The PHP has to run a complex set of tasks based on the variables passed,so it can, at times take upwards of 3-5 seconds. Deosnt seem like much, but it's a big issue for our customers.So, I need a way that the PHP can say "ok, I have the variables, let the browser go on without me". Just like the php filename.php & does at the CLI.Note: The PHP does not provide anything back to the browser. It's purely a script with no http output. Quote Link to comment Share on other sites More sharing options...
trq Posted August 16, 2006 Share Posted August 16, 2006 A simple method might be to look at executing this script through exec. eg;[code=php:0]exec("/path/to/script.php {$_GET['var1']} {$_GET['var2']} &");[/code]Another might be to look into forking, though I wouldn't recommend trying that from within a server process either. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 16, 2006 Share Posted August 16, 2006 jvalarta.....The cron job IS your answer....you can set an infinite loop in a script in a cron job too!but really - what is wrong in doing something where a cron job kicks off every minute, the loop is...for($i = 0 ; $i <3 ; $i++){ whatever your code is sleep (20);}that loop will run once every 20 seconds (so in 1 minute it will execute at 00 20 and 40 seconds only to be restarted at the begining of the next minute).Unless your data feed is lost the instance it gets to you (that is stupid and if so even an infinite loop will more than probably miss something!!!) then there is no detectable difference to the user whether it picks up data every nanosecond or every 20 seconds.Setting the cron job will leave this process running until you kill it manually - it is NOT dependant on a browser window being open.. Quote Link to comment Share on other sites More sharing options...
jvalarta Posted August 16, 2006 Author Share Posted August 16, 2006 So I did a test with a cron running once every minute and the reason this didnt work in my situation is that the PHP execution time varies between .5 to up to 3-4 seconds...so if a cron runs once a minute, and the PHP is still processing past the 59th second, the cron starts another process (PID) and then I have 2 running at the same time, which will open up a data integrity issue.There has got to be a way to have PHP start running and then release the browser -- and if there is, then this problem is solved and I dont need the daemon.Here's my thinking in a super basic example:<?//script starts - accepts variables//script releases browsersleep(100); //script continues running?>If this PHP was executed, the browser should immediately be done loading, even though the PHP is still running/peforming (in this example, a sleep). Quote Link to comment Share on other sites More sharing options...
trq Posted August 16, 2006 Share Posted August 16, 2006 Did you read my last post? Quote Link to comment Share on other sites More sharing options...
jvalarta Posted August 17, 2006 Author Share Posted August 17, 2006 Yes, I meant to comment on this. I tried the exec method -- I kept recieving "unable to fork" PHP error and all references I found to this error were Windows related, and I'm on a LAMP box.Any suggestions? I do think this just might be the solution! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 17, 2006 Share Posted August 17, 2006 alter the sleep to 18 instead fo 20. or what ever value you need!I personally would loop just twice and leave sleep at 30. Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 17, 2006 Share Posted August 17, 2006 Best advice: do NOT use PHP for this type of situation. PHP is not designed for this and will have dramatic performance implications.Redesign your system so that it does not require a 24/7/365 listener. I find it hard to believe any web-based application requires one. Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted August 17, 2006 Share Posted August 17, 2006 worked on one previously and didn't suffer that much in terms of performance - the only problem being if the script hung. BUT I have seen a asp app (think it was in c#) that had an object monitor - this only kicked in when something changed in another object so would be a far more useful tool in this situation - haven't seen anything similar in php yet. Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 17, 2006 Share Posted August 17, 2006 That sounds like an Observer.If you want to learn more about it, google for "Observer Design Pattern"Listeners are bad.. trigger the job when it is needed, not have it on standby. Quote Link to comment Share on other sites More sharing options...
trq Posted August 17, 2006 Share Posted August 17, 2006 Ive stated all this previously. Creating a deamon in php is fine, heck I run two web servers written in php, but trying to create a deamon within another servers process is a bad idea. Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 17, 2006 Share Posted August 17, 2006 No, trying to run an application that is a scripting app as a daemon is a bad idea.Apache is the daemon, PHP is not. Quote Link to comment Share on other sites More sharing options...
trq Posted August 17, 2006 Share Posted August 17, 2006 I run two web servers written entirely in php. I dont have Apache installed. Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 17, 2006 Share Posted August 17, 2006 I call shennanigans. PHP is incapable of running on it's own as it is from php.net. Quote Link to comment Share on other sites More sharing options...
trq Posted August 17, 2006 Share Posted August 17, 2006 [quote]I call shennanigans.[/quote]What? [url=http://nanoweb.si.kz/]nanoweb[/url]. Ive found nanoweb to be faster than apache in allot of cases. Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 17, 2006 Share Posted August 17, 2006 thanks for misquoting. Take a read at the rest of it.nanoweb is not default PHP like 99% of php hosts have. It is a custom made webserver, thus only hosts that install nanoweb can run 'php' (which it isn't, it's nanoweb) as a daemon.Therefore, it is of no use to this topic. Quote Link to comment Share on other sites More sharing options...
trq Posted August 17, 2006 Share Posted August 17, 2006 What? All you need do is install php cli, and start the nanoweb daemon. It IS a web server written in php. You where stating the fact that php can't create (or is no good at) deamons, I am saying you are incorrect. Quote Link to comment Share on other sites More sharing options...
Jenk Posted August 17, 2006 Share Posted August 17, 2006 You make it sound so easy.. when it's not.Most hosts refuse to change a simple setting in php.ini, let alone install a completely different setup to what they already have. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.