Jump to content


Photo

PHP Daemon?


  • Please log in to reply
25 replies to this topic

#1 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 15 August 2006 - 04:35 AM

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?

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 August 2006 - 09:24 AM

Just put the program into an infinant loop....

$a = TRUE;
while ($a) {
  // do your database lookups in here
  // you might also want to use the sleep function
  sleep(5);
}

Yes it will use processing power. Will it kill your machine? No, it should be fine. Assuming your using Linux, use the kill command to end the process. Id'e even write a bash wrapper around it so it can be killed easily.

#3 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 15 August 2006 - 09:59 AM

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).
follow me on twitter @PHPsycho

#4 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 15 August 2006 - 08:37 PM

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?!

#5 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 15 August 2006 - 10:24 PM

The second I close the window, the script stops.


Run it in the background. eg;
path/to/your/script.php &


#6 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 15 August 2006 - 11:33 PM

??? 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?

#7 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 August 2006 - 12:03 AM

Like, if I wanted to kick off a script via the Web, and do this same thing?


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.

#8 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 16 August 2006 - 12:29 AM

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.

#9 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 August 2006 - 12:35 AM

A simple method might be to look at executing this script through exec. eg;

exec("/path/to/script.php {$_GET['var1']} {$_GET['var2']} &");

Another might be to look into forking, though I wouldn't recommend trying that from within a server process either.

#10 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 16 August 2006 - 12:40 AM

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..
follow me on twitter @PHPsycho

#11 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 16 August 2006 - 06:25 AM

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 browser

sleep(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).

#12 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 August 2006 - 01:22 PM

Did you read my last post?

#13 jvalarta

jvalarta
  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 17 August 2006 - 02:47 PM

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!

#14 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 17 August 2006 - 03:08 PM

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.
follow me on twitter @PHPsycho

#15 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 17 August 2006 - 03:15 PM

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.


#16 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 17 August 2006 - 03:19 PM

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.
follow me on twitter @PHPsycho

#17 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 17 August 2006 - 03:23 PM

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.

#18 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 17 August 2006 - 03:33 PM

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.

#19 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 17 August 2006 - 03:34 PM

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.

#20 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 17 August 2006 - 03:36 PM

I run two web servers written entirely in php. I dont have Apache installed.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users