Xavier
Xavier

Reputation: 123

How can I run a daemon on xampp using PHP?

I have an XML database that I want to manage independently from users on my website. Looking into the matter it appears that I should write a daemon script to manage my database. That is all fine and dandy but, I feel like I'm opening a can of worms. I wanted to write my daemon script in PHP, so I looked into PCNTL. But I quickly learned that PCNTL is not suited for web servers. So now I am stumped. How can I get a daemon to run on my server? Do I need to learn another language? I only want to write my own scripts. But I feel lost. I would prefer to write my daemon in PHP as I am familiar with the language.

I have been researching everything from PCNTL, CLI, SO questions, numerous articles on daemon processes... etc

I am running PHP 5.6.32 (cli), windows 7, on Apache. XAMPP 5.6.32. Unix system.

EDIT: I also have windows setup to run PHP from command prompt.

Upvotes: 0

Views: 534

Answers (1)

GrowingBrick
GrowingBrick

Reputation: 751

There's nothing wrong in running a PHP daemon, however it's not the fastest thing, especially before the 7.0 version. You can proceed in two ways:

  • Using Cron Jobs, if you're under Unix systems crontab will be fine, in this way you can specify the interval within the system automatically executes the specified script and then exit.
  • The true daemon, firstly you need to change the max_execution_time in PHP.ini to 0 (infinite), then in your daemon call for first function set_time_limit(0);, remember to run it only once. However if there is some failure like a thrown error uncatched the script will exit and you need to open it again manually, and don't try...catch in a while loop because it will probably go into an endless loop. Execute the script with php -f daemon.php.

Upvotes: 1

Related Questions