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
Www.2cto.com
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
Ignore_user_abort (); // The PHP script can be executed even if the Client is disconnected (such as the browser is disabled.
Set_time_limit (0); // the execution time is unlimited. The default php Execution time is 30 seconds. You can use set_time_limit (0) to limit the execution of the program.
$ Interval = 60*5; // run every 5 minutes
Do {
$ Fp = fopen('test.txt ', 'A ');
Fwrite ($ fp, 'test ');
Fclose ($ fp );
Sleep ($ interval); // wait for 5 minutes
} While (true );
?>
<? Php
Ignore_user_abort (); // The PHP script can be executed even if the Client is disconnected (such as the browser is disabled.
Set_time_limit (0); // the execution time is unlimited. The default php Execution time is 30 seconds. You can use set_time_limit (0) to limit the execution of the program.
$ Interval = 60*5; // run every 5 minutes
Do {
$ Fp = fopen('test.txt ', 'A ');
Fwrite ($ fp, 'test ');
Fclose ($ fp );
Sleep ($ interval); // wait for 5 minutes
} While (true );
?>
As long as you run the above page, and then turn it off, the program will continue to run.
From God's blog