Php simulates linux crontab to implement scheduled tasks

Source: Internet
Author: User

Php simulates linux crontab to implement scheduled tasks

This article describes how to implement scheduled tasks by simulating linux crontab in php.

PHP scheduled tasks require two files.

1. crontab. conf. php (configuration file)

<? Php // when the value is 0, close the scheduled task return 1;

2. crontab. php (subject file)

<? Phpignore_user_abort (); // close the browser and run the PHP script. set_time_limit (0); // you can use set_time_limit (0) to run the program without restrictions $ interval = 60*3; // run the program every 3 minutes $ I = 0; do {$ nowTime = date ("Y-m-d H: m: s"); $ run = include 'crontab. conf. php'; if (! $ Run) {file_put_contents ("crontab_task.log", "= scheduled task ended $ nowTime = \ r \ n", FILE_APPEND); die ('job has ended. ');} // put the code to be executed here sleep ($ interval); // wait for 3 minutes $ data = "$ I> the scheduled task is running... (running status: $ run) $ nowTime \ r \ n "; file_put_contents (" tasktest.txt ", $ data, FILE_APPEND); $ I ++;} while (true );

Mainly change the return 0 in crontab. conf. php to close the scheduled task.

Note: When the php service is turned off, the scheduled task script will also be stopped and will not be restarted automatically.

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.