PHP Execution Plan Task

Source: Internet
Author: User

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

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.