Jump to content

Recommended Posts

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?
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/#findComment-74995
Share on other sites

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?!
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/#findComment-75317
Share on other sites

??? 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?
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/#findComment-75408
Share on other sites

[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.
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/#findComment-75431
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/#findComment-75449
Share on other sites

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..
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/#findComment-75452
Share on other sites

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).
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/#findComment-75533
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/17583-php-daemon/#findComment-76271
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.