This article describes the principles and implementation methods of php script daemon. Share it for your reference, as follows:
Ideas:
1. While loop, if there is currently no data to operate, it can sleep;
2. The crontab script executes the script every fixed time period. When executing, check whether it is already being executed. If there is no execution, skip it.
3. nohup background execution
4. flock -xn Add lock
Example:
To execute code:
<?php set_time_limit(0); //The dead cyclewhile(1) { $message = '1111111' . "\n"; error_log($message); sleep(5); }
#/tmp/lock/ For the file to be locked by the current process, different processes configure different lock files, and the file will be automatically created.* * * * * flock -xn /tmp/lock/ -c 'nohup php >> /php/ 2>&1 &' * * * * * flock -xn /tmp/ -c 'php /home/fdipzone/php/ >> /home/fdipzone/php/'
Writing a php script. To prevent daemon memory overflow, it is recommended to regularly detect memory usage.
Put the following code into the business script:
if(memory_get_usage()>100*1024*1024){ exit('Memory overflow');//Exit the program by memory greater than 100M to prevent memory leakage from being killed by the system, causing the task terminal to be}
Notice:
Nohup task viewing and closing methods:
closure:
//Method 1:ps -e | grep commend kill -9 pid //Method 2:fg %n //n is the process number viewed by the jobs command
Check:
//View background processjobs
principle:
Use linux flock file lock to implement task locking and resolve conflicts
Format:
flock [-sxun][-w #] fd# flock [-sxon][-w #] file [-c] command
Options
-s, --shared: Get a shared lock
-x, --exclusive: Obtain an exclusive lock
-u, --unlock: Removing a lock is usually not needed. The lock will be automatically discarded after the script is executed.
-n, --nonblock: If the lock is not obtained immediately, it will fail directly instead of waiting
-w, --timeout: If the lock is not obtained immediately, wait for the specified time
-o, --close: Descriptor symbol for closing the file before running the command. Used for controls that will not be locked if a command generates a child process.
-c, --command: Run a separate command in the shell
-h, --help Show Help
-V, --version: Display version
Run a php file, the file lock uses an exclusive lock, and if locked, it fails and does not wait. Parameter is -xn
* * * * * flock -xn /tmp/ -c 'php /home/fdipzone/php/ >> /home/fdipzone/php/'
In this way, when the task is not completed and the next task is determined to be /tmp/ locked, the current task is terminated and the next cycle is judged.
For more information about PHP related content, please check out the topic of this site:Summary of PHP process and thread operation skills》、《Summary of PHP network programming skills》、《Introduction to PHP basic syntax》、《Complete collection of PHP array (Array) operation techniques》、《Summary of usage of php strings》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php》
I hope this article will be helpful to everyone's PHP programming.