Home php教程 php手册 PHP定时执行任务/Cron Job

PHP定时执行任务/Cron Job

Jun 13, 2016 am 10:17 AM
cron job php Task timing implement perform tasks yes solution

对于PHP本身并没有一套解决方案来执行定时任务,不过是借助sleep函数完成的。这种方就是要提前做一些配置,如实现过程:

<span ignore_user_abort</span>();<span //</span><span 关掉浏览器,PHP脚本也可以继续执行.</span>
    <span set_time_limit</span>(0);<span //</span><span  通过set_time_limit(0)可以让程序无限制的执行下去</span>
    <span $interval</span>=60*30;<span //</span><span  每隔半小时运行</span>
    <span do</span><span {
        </span><span //</span><span 这里是你要执行的代码   </span>
        <span sleep</span>(<span $interval</span>);<span //</span><span  等待5分钟</span>
    }<span while</span>(<span true</span>);
Copy after login

不过关于这种方式我有一些担心性能方面的问题,不过也是个临时的方法。

本人推荐的方式采用脚本来实现,利用OS本身的定时任务机制,windows上场利用bat脚本。不过本人没有在window上尝试过。那我就讲讲在linux中的实现。

如果你的web server是基于linux的,那么可以采用linux下的cron job 来完成。以RedHat5 为例,我们只需要预先要定时执行的逻辑代码。例如demo.php

<?<span php
</span><span echo</span> "Hello"<span ;
</span>?>
Copy after login

然后是吧php用shell脚本一封装,在shell脚本中调用demo.php,demo.sh代码如下:

#!/bin/<span bash
</span><span #if</span> you php install to /usr/local/php/
/usr/local/php/bin/php /home/xx-user/demo.php
Copy after login

完成shell脚本的编写之后,确保它有足够的这行权限,例如:/bin/chmod u+x demo.sh.

然后配置linux上的cronjob,cronjob是linux上默认安装的。如果你的任务是需要按小时、天、周、月来执行的话,那么你直接可以将你的demo.sh脚本拷贝到

/etc/cron.hourly 、/etc/cron.daily 、/etc/cron.weekly、/etc/cron.monthly 就ok了,这样就可以完成你的任务了 。如果到某个时间点想移除定时任务,那就从上面这些folder中move到其它地方或者直接删除掉就ok了。

如果你的脚本有特殊的执行时间,例如每个星期的周二,或者是每个月的15号执行。那么你就需要配置属于你自己的cron job.

关于cron的特殊的配置请参考:http://www.pantz.org/software/cron/croninfo.html

这里我就按照每天的上午12点到下午的14点钟之间每2分钟运行一次这个脚本,那么配置如下(例如demo.sh是位于/tmp 目录下):

首先在linux的命令行中执行crontab -e ,然后把规则输入到里面:

*/<span 2</span> <span 12</span>-<span 14</span> * * *  /tmp/demo.<span sh</span>
Copy after login

带输入完成之后按键盘上的“Esc”键,然后输入:wq,编辑页面就退出了。然后你可以用crontab -l 来查看你刚编辑的cron job.

到此时特殊的cron 也就完成了。例如你刚才是用linux下的demo账户来完成上面的步骤,那么还有一个简单的方法就是直接可以编辑/var/spool/cron/demo 这个文件,可以直接

修改你的cron job.  例如 :vi /var/spool/cron/demo

利用OS的方式来管理你的定时任务是很快捷的,而且不用你担心性能的问题,除非你的脚本本身存在一些问题。这样的方式易于维护,可以修改定时执行的计划,也可以轻松的移除和新增其它的定时任务。

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

See all articles