Implement the daemon process on a Linux terminal. You can use crontab to plan PHP script execution.
The crontab command schedules the execution of some commands at certain intervals.
Crontab usage: crontab [-E |-L |-R] File Name-E: Edit task-L: Display task information-R: delete scheduled execution task information
Crontab format:
* *** Command
Command to be run by hour, day, month, and week
"*" Indicates all numbers in the value range, "/" indicates each meaning, and "*/5" indicates every five units, "-" represents a number from a number to a number, "," separate several discrete numbers.
1. directly use the crontab command to edit
Crontab allows you to schedule tasks for users.
Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.
Crontab-l // list the details of a user's cron Service
Crontab-r // delete a user's cron Service
Crontab-E // edit a user's cron Service
For example, to view your cron settings as root: crontab-u root-l
For another example, Root wants to delete Fred's cron settings: crontab-u Fred-R
When editing the cron service, edit the cron settings of the root user, enter crontab-u root-E, and enter the VI editing mode. The edited content format is *** command
For example, to execute a task:
The task needs to run the PHP script once every minute, record the execution time, and save it to result. log.
Start. php:
#! /Usr/bin/PHP
<? PHP
File_put_contents ('result. log', 'current running time: '. date ("Y-m-d", time (). "/R/N", file_append );
Shell. sh:
#! /Bin/sh
PHP start. php
Terminal:
Crontab-u root-e
# Enter the vim editing mode:
*/1 * shell. Sh
: WQ exit and save
In addition, the PHP script can be executed even if the client is disconnected (for example, the browser is closed) on the web page.
The ignore_user_abort () function can be used with set_time_limit (0) and sleep ($ interval) to implement the program.
<? Php <br/> ignore_user_abort (); // The PHP script can be executed even if the client is disconnected (such as closing the browser. <br/> set_time_limit (0); // the execution time is unlimited. The default PHP Execution time is 30 seconds) the program can be executed without limit <br/> $ interval = 60*5; // run every 5 minutes <br/> do {<br/> $ fp = fopen('test.txt ', 'A'); <br/> fwrite ($ FP, 'test'); <br/> fclose ($ FP); <br/> sleep ($ interval ); // wait for 5 minutes <br/>}while (true); <br/>?>
As long as you run the above page, and then turn it off, the program will continue to run.