PHP-based crontab scheduled task management

Source: Internet
Author: User
PHP-based crontab scheduled task management by jenner · August 6

Linux crontab has always been a powerful tool for server O & M and business development. However, when the number of scheduled tasks increases, management and migration become troublesome and prone to problems. The following provides a crontab manager written in php, but you still need to add a one-minute execution command under crontab to run this manager. Through this manager, you can achieve the following goals:

  • Centralized management of distributed scheduled tasks
  • Merge multiple crontab records
  • Crontab record persistent storage (configuration file or database)
  • It is not recommended that you use the database for crontab configuration management, unless you can ensure that database requests can maintain stable response for a long period of time. We recommend that you use nosql cache storage and make persistent backups.

    The previous test code is as follows:

    Define ('Ds', DIRECTORY_SEPARATOR); requiredirname (_ FILE __). DS. 'endand '. DS. 'autoload. php '; date_default_timezone_set ('prc'); error_reporting (E_ALL); $ crontab_config = ['test _ 1' => ['name' => 'service monitoring 1 ', 'cmd' => 'php-v', 'output' => '/tmp/test. log', 'Time' => '*****'], 'Single _ test' => ['name' => 'php-I ', 'cmd' => 'php-I ', 'output' =>'/tmp/single_script.log ', 'Time' => ['*****', '*****',]; $ crontab_server = new \ Jenner \ Zebra \ Crontab ($ crontab_config); $ crontab_server-> start ();

    This code uses the php package manager composer. if you do not know, you can manually include the classes you need to use to your php script.

    After running, we will view the running records of crontab in the default log file (/var/log/php_crontab.log, of course, you can specify the log file by passing the second parameter to Crontab (ensure that the log file is writable ). The log file content is as follows:

    [2014-11-10 19:50:08]-content:start. pid3778[2014-11-10 19:50:08]-content:php -v[2014-11-10 19:50:08]-content:php -i[2014-11-10 19:50:08]-content:php -i[2014-11-10 19:50:08]-content:end. pid:3778

    The log records the startup time, running command, pid, and other information of the program. Since I executed it manually, the description is not accurate for 00 seconds. During official use, add the following command to the crontab to automatically run the manager.

    * * * * * php php_crontab_manager.php

    Manager dependencies:

  • Process control package: "jenner/multi_process": "1.0.0 ",
  • Pcntl extension
  • Crontab service
  • The jenner/multi_process package is a simple process control package. it is mainly used to execute scheduled tasks using sub-processes, so that the parent process will not be blocked, resulting in scheduled task latency.

    Project address:

    This project is hosted on github and supports packagist packages. you can add "jenner/crontab": "1.0.0" to composer. json to load this package.

    The specific source code can be viewed on github.

    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.