php+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 *

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.
