Principles and implementation methods of the php script daemon

Source: Internet
Author: User

Principles and implementation methods of the php script daemon

This article describes the principles and implementation of the php script daemon. We will share this with you for your reference. The details are as follows:

Ideas:

1. while LOOP, if there is no data to operate, you can sleep;
2. The crontab script executes the script every fixed period of time. During execution, it first checks whether the script is being executed. If no execution is performed, It is skipped.
3. nohup background execution
4. flock-xn locking

Instance:

Code to be executed: index. php

<? Phpset_time_limit (0); // infinite loop while (1) {$ message = '000000'. "\ n"; error_log ($ message); sleep (5 );}
#/Tmp/lock/test1.lock is the file to be locked by the current process. Different processes configure different lock files, this file will automatically create ***** flock-xn/tmp/lock/test1.lock-c 'nohup php index. php>/php/test. log 2> & 1 & '***** flock-xn/tmp/mytest. lock-c 'php/home/fdipzone/php/test. php>/home/fdipzone/php/test. log'

In the written php script. To prevent memory overflow of the daemon, we recommend that you periodically check the memory usage.
Put the following code in the business script:

If (memory_get_usage ()> 100*1024*1024) {exit ('memory overflow '); // exit the program with a memory larger than MB to prevent the task terminal from being killed by the system due to memory leakage}

Note:

How to view and close a nohup task:

Close:

// Method 1: ps-e | grep commendkill-9 pid // Method 2: fg % n // n is the process number viewed by the jobs command

View:

// View the background process jobs

Principle:

Use linux flock file lock to lock tasks and resolve conflicts

Format:

flock [-sxun][-w #] fd#flock [-sxon][-w #] file [-c] command

Option

-S, -- shared: Get a shared lock
-X, -- exclusive: obtains an exclusive lock.
-U, -- unlock: Remove a lock, which is usually not required. The lock is automatically discarded after the script is executed.
-N, -- nonblock: If the lock is not obtained immediately, direct failure rather than waiting
-W, -- timeout: If the lock is not obtained immediately, wait for the specified time
-O, -- close: close the file description before running the command. Used to control sub-processes that are not locked when a command is generated.
-C, -- command: run a separate command in shell
-H, -- help show help
-V, -- version: display version

Run a PHP file and use an exclusive lock for the file lock. If the file lock fails, do not wait. The parameter is-xn.

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

In this way, when the task is not completed, the next task determines that/tmp/mytest. lock is locked, the current task is ended, and the next cycle is judged again.

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.