Daemon-php daemon does not die but does not work after running for a while

Source: Internet
Author: User
Tags php cli
I used while (true) to run a daemon in the background. The task of the process is to read the interface and put the interface content into Mysql & amp; redis & amp; json. js. The problem is that after the process runs for a period of time (about half a month), it does not die, but does not work. This is a regular... I used while (true) to run a daemon in the background. The task of the process is to read the interface and put the interface content into Mysql & Redis & json. js.

The problem is that after the process runs for a period of time (about half a month), it does not die, but does not work.

Is this a common problem? It is also caused by a problem with my code.

My current solution is to regularly restart the process. Is there a better solution?

Some code

public function sync() {    while(true){        $this->syncData();        sleep(5);    }}

Reply content:

I used while (true) to run a daemon in the background. The task of the process is to read the interface and put the interface content into Mysql & Redis & json. js.

The problem is that after the process runs for a period of time (about half a month), it does not die, but does not work.

Is this a common problem? It is also caused by a problem with my code.

My current solution is to regularly restart the process. Is there a better solution?

Some code

public function sync() {    while(true){        $this->syncData();        sleep(5);    }}

We recommend that you do not use persistent connections when writing a php cli program running in the command line. We recommend that you re-open and close the database connection every time you use MySQL, avoid CLI program failure when database connection is lost. redis is the same.

The workerman or swoole framework is suitable for daemon tasks. Both are active and there are many users.

Use shell's while to replace PHP's while

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.