PHP Script daemon principle and implementation code

Source: Internet
Author: User
Tags flock
This paper mainly introduces the principle and implementation method of PHP script daemon, analyzes the implementation idea, principle, format and implementation method of PHP script daemon in detail, and needs friends to make reference, hoping to help everyone.

Ideas:

1. While loop, if there is no data to operate can sleep;
2. The crontab script executes the script every fixed period of time, and when executed, detects if it is already executing and skips if no execution occurs.
3. Nohup Background execution
4. FLOCK-XN Plus Lock

Instance:

To execute code: index.php


<?phpset_time_limit (0);//Dead Loop while (1) {  $message = ' 1111111 '. "\ n";  Error_log ($message);  Sleep (5);}


#/tmp/lock/test1.lock for the current process to lock files, different processes configure different lock files, the file is automatically created * * * * * * flock-xn/tmp/lock/test1.lock-c ' nohup php index.php & Gt;>/php/test.log 2>&1 & ' * * * * * * flock-xn/tmp/mytest.lock-c ' php/home/fdipzone/php/test.php >> /home/fdipzone/php/test.log '

The PHP script that was written well. To prevent daemon memory overflow, we recommend that you periodically detect memory consumption.
Place the following code in the business script:


if (Memory_get_usage () >100*1024*1024) {  exit (' memory overflow '),///greater than 100M memory exit program to prevent memory leaks from being killed by the system causing the task terminal}

Attention:

Nohup task View and shutdown method:

Shut down:


Method One: Ps-e | grep commendkill-9 pid//Method Two: FG%n  //n The process number viewed for the jobs command

View:


View Background Process jobs

Principle:

Task locking with Linux flock file locks for conflict resolution

Format:


Flock [-sxun][-w #] Fd#flock [-sxon][-w #] file [-c] Command

Options

-S,--shared: Get a shared lock
-X,--exclusive: Get an exclusive lock
-U,--unlock: Removes a lock, usually unwanted, and automatically discards the lock when the script finishes executing
-N,--nonblock: If the lock is not immediately acquired, it fails directly instead of waiting
-W,--timeout: Wait for the specified time if the lock is not immediately available
-O,--close: Closes the description of the file before running the command. Used if the command produces child processes that are not locked by the control
-C,--command: Run a separate command in the shell
-H,--help display Help
-V,--version: Display version

Run a php file, the file lock uses an exclusive lock, and if the lock fails to wait. parameter is-XN

* * * * * * flock-xn/tmp/mytest.lock-c ' php/home/fdipzone/php/test.php >>/home/fdipzone/php/test.log '

This way, when the task is not completed, the next task determines that the/tmp/mytest.lock is locked, then ends the current task and then judges the next cycle.

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.