search
HomeBackend DevelopmentPHP Tutorialphp+crontab的定时任务-- 完结版

php+crontab的定时任务,看网上各种资料,总是他一言你一语,没有一篇文章,看完就让人懂的。现总结如下:

一、crontab是linux系统功能与程序无关

    crontab -e //编辑某个用户的cron服务                //这个最重要,自己编写crontab

    crontab -l //列出某个用户cron服务的详细内容   //这个也重要,查看自己写了哪些定时任务

    crontab -u //设定某个用户的cron服务,一般root用户在执行这个命令的时候需要此参数
    crontab -r //删除某个用户的cron服务


  关于时间的书写规则:

    前5个字段分别表示:
    分钟:0-59
    小时:1-23
    日期:1-31
    月份:1-12
    星期:0-6(0表示周日)

    还可以用一些特殊符号:
      *: 表示任何时刻
      ,: 表示分割
      -:表示一个段,如第二端里: 1-5,就表示1到5点
      /n : 表示每个n的单位执行一次,如第二段里,*/1, 就表示每隔1个小时执行一次命令。也可以写成1-23/1.

二、写完重启cron:
  sudo service cron restart 或者 /etc/init.d/cron restart 

三、对于Yii下使用cron:

  重点三个地方:

  A/  protected/config/console.php  -->里面db注释的部分开启

  B/ 自己建commands/TestCommand.php

  C/ yiic.php里面的引用地址的配置,细节见补充

写一个运行成功的实例:

    */1 * * * * php /home/user_name/sites/project_name/yiicmd.php test 'datetime'

    site为用户名下的php运行目录,project_name为项目名。yiicmd.php 自己写的文件,与protected下的yiic.php一样,但里面yii的引用路径要改~

关于cron的补充:

  cron的实际存放地址:看着好像etc下面有cron.d   cron.daily  cron.hourly  crontab,这么多crontab的文件,但实际上:

    linux:/var/spool/cron     // 用 crontab -l 打开看到的文件,就是它

    freebsd:/var/cron/tabs

关于yiic补充:

yiic 默认只是一个php文件,需要用php命令运行:php yiic 或者 php yiic.php

也可以用:chmod +x yiic 命令直接将yiic改成可运行的。 便可直接进入cd protected写:yiic help。


参考资料:
http://www.yiiframework.com/extension/yii-crontab/               //关于yiicmd.php位置无所谓,都可以,关键里面定位要注意。这个插件几乎无用,因为它无法写入crontab中,要改掉各种文件的写权限,太烦。
http://linux.chinaitlab.com/unix/795992.html                          //讲的也比较好,简洁
http://blog.csdn.net/tianlesoftware/article/details/5315039   //讲的一般,比较细

 

几个典型时间例子:
每五分钟执行 */5 * * * *
每小时执行 0 * * * *
每天执行 0 0 * * *
每周执行 0 0 * * 0
每月执行 0 0 1 * *
每年执行 0 0 1 1 *

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool