Home Backend Development PHP Tutorial linux使用crontab实现PHP执行计划定时任务_PHP

linux使用crontab实现PHP执行计划定时任务_PHP

Jun 01, 2016 am 11:53 AM
crontab scheduled tasks

首先说说cron,它是一个linux下的定时执行工具。根用户以外的用户可以使用 crontab 工具来配置 cron 任务。所有用户定义的 crontab 都被保存在/var/spool/cron 目录中,并使用创建它们的用户身份来执行。要以某用户身份创建一个 crontab 项目,登录为该用户,然后键入 crontab -e 命令来编辑该用户的 crontab。该文件使用的格式和 /etc/crontab 相同。当对 crontab 所做的改变被保存后,该 crontab 文件就会根据该用户名被保存,并写入文件 /var/spool/cron/username 中。cron 守护进程每分钟都检查 /etc/crontab 文件、etc/cron.d/ 目录、以及 /var/spool/cron 目录中的改变。如果发现了改变,它们就会被载入内存。这样,当某个 crontab 文件改变后就不必重新启动守护进程了。

安装crontab:

yum install crontabs

说明:
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置

查看crontab服务状态:service crond status

手动启动crontab服务:service crond start

查看crontab服务是否已设置为开机启动,执行命令:ntsysv

加入开机自动启动:
chkconfig –level 35 crond on

crontab命令:

功能说明:设置计时器。

语  法:crontab [-u ][配置文件] 或 crontab [-u ][-elr]

补充说明:cron是一个常驻服务,它提供计时器的功能,让用户在特定的时间得以执行预设的指令或程序。只要用户会编辑计时器的配置文件,就可以使 用计时器的功能。其配置文件格式如下:
Minute Hour Day Month DayOFWeek Command

参  数:
-e  编辑该用户的计时器设置。
-l  列出该用户的计时器设置。
-r  删除该用户的计时器设置。
-u  指定要设定计时器的用户名称。

crontab 格式:

基本格式 :

分钟   小时   日   月   星期   命令

*        *      *    *     *       *

第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列 表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令

记住几个特殊符号的含义:
“*”代表取值范围内的数字,
“/”代表”每”,
“-”代表从某个数字到某个数字,
“,”分开几个离散的数字

# Use the hash sign to prefix a comment
# +—————- minute (0 – 59)
# | +————- hour (0 – 23)
# | | +———- day of month (1 – 31)
# | | | +——- month (1 – 12)
# | | | | +—- day of week (0 – 7) (Sunday=0 or 7)
# | | | | |
# * * * * * command to be executed

crontab几个例子如下:

(1)第一个例子。

30 21 * * * /etc/init.d/nginx restart
每晚的21:30重启 nginx。

(2)第二个例子,也就是本教程测试的例子

* * * * * /usr/bin/php -f /root/test.php >> test.log

每一分钟执行/root/test.php文件,将结果输出到test.log中。

完成了上面基础工作后,就来看看怎么使用crontab定时执行PHP脚本:

(1)我在/root下新建test.php文件,内容如下:

复制代码 代码如下:
        #!/usr/bin/php -q
        echo  date('Y-m-d H:i:s')."from http://www.phpddt.com \n";
?>

说明:你可以用whereis php查找php执行文件位置。


(2)然后crontab -e编写如下shell:

复制代码 代码如下:
* * * * * /usr/bin/php -f /root/test.php >> test.log

说明:test.php必须为可执行文件:chmod +x test.php

测试结果很正常,截图如下:

linux使用crontab实现PHP执行计划定时任务_PHP

当然你可以用使用crontab -e继续添加任务,在/var/spool/cron下你可以看到一个root文件。
windows下直接用windows计划任务,通过bat打开网页就可以了。不像linux这么复制。

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

Video Face Swap

Video Face Swap

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

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)

Do you know some reasons why crontab scheduled tasks are not executed? Do you know some reasons why crontab scheduled tasks are not executed? Mar 09, 2024 am 09:49 AM

Summary of some reasons why crontab scheduled tasks are not executed. Update time: January 9, 2019 09:34:57 Author: Hope on the field. This article mainly summarizes and introduces to you some reasons why crontab scheduled tasks are not executed. For everyone Solutions are given for each of the possible triggers, which have certain reference and learning value for colleagues who encounter this problem. Students in need can follow the editor to learn together. Preface: I have encountered some problems at work recently. The crontab scheduled task was not executed. Later, when I searched on the Internet, I found that the Internet mainly mentioned these five incentives: 1. The crond service is not started. Crontab is not a function of the Linux kernel, but relies on a cron.

How to use Systemd and Crontab to implement parallel execution of tasks in Linux systems How to use Systemd and Crontab to implement parallel execution of tasks in Linux systems Sep 26, 2023 pm 06:37 PM

How to use Systemd and Crontab to implement parallel execution of tasks in a Linux system. In a Linux system, parallel execution of tasks is one of the important means to improve system efficiency and performance. This article will introduce how to use Systemd and Crontab tools to implement parallel execution of tasks in a Linux system, and provide specific code examples. 1. Introduction to Systemd Systemd is a tool used to manage the startup process and service management of Linux systems. via configuration

ThinkPHP6 scheduled task scheduling: scheduled task execution ThinkPHP6 scheduled task scheduling: scheduled task execution Aug 12, 2023 pm 03:28 PM

ThinkPHP6 scheduled task scheduling: scheduled task execution 1. Introduction In the process of web application development, we often encounter situations where certain repetitive tasks need to be executed regularly. ThinkPHP6 provides a powerful scheduled task scheduling function, which can easily meet the needs of scheduled tasks. This article will introduce how to use scheduled task scheduling in ThinkPHP6, and provide some code examples to help understand. 2. Configure scheduled tasks, create scheduled task files, and create a comman in the app directory of the project.

How to perform task scheduling and scheduled tasks in PHP? How to perform task scheduling and scheduled tasks in PHP? May 12, 2023 pm 06:51 PM

In web development, many websites and applications need to perform certain tasks regularly, such as cleaning up junk data, sending emails, etc. In order to automate these tasks, developers need to implement task scheduling and timed task functions. This article will introduce how to implement task scheduling and timed tasks in PHP, as well as some commonly used third-party libraries and tools. 1. Task Scheduling Task scheduling refers to executing certain tasks according to specified times or events. In PHP, cron timer or similar mechanism can be used to implement task scheduling. Typically, task scheduling

How to solve the pitfalls of commenting crontab files and crontab executing sh in Linux How to solve the pitfalls of commenting crontab files and crontab executing sh in Linux May 15, 2023 pm 09:58 PM

Linux annotation crontab files and crontab execution sh pitfalls. It turns out that many crontabs are written under Linux to perform certain tasks regularly. Now there are the following requirements: Requirement: It is to annotate certain crontab tasks. Method: Just add the crontab to be canceled. Just add '#' before the task. e.g.54**sunecho"runat5after4everysunday"Comment: #54**sunecho"runat5after4everysunday"It's that simple. Encounter pit 1, look at the following example recently

Python implements automatic page refresh and scheduled task function analysis for headless browser collection applications Python implements automatic page refresh and scheduled task function analysis for headless browser collection applications Aug 08, 2023 am 08:13 AM

Python implements automatic page refresh and scheduled task function analysis for headless browser collection applications. With the rapid development of the network and the popularization of applications, the collection of web page data has become more and more important. The headless browser is one of the effective tools for collecting web page data. This article will introduce how to use Python to implement the automatic page refresh and scheduled task functions of a headless browser. The headless browser adopts a browser operation mode without a graphical interface, which can simulate human operation behavior in an automated way, thereby enabling the user to access web pages, click buttons, and fill in information.

How to use scheduled tasks in FastAPI to perform background work How to use scheduled tasks in FastAPI to perform background work Jul 28, 2023 pm 02:22 PM

How to use scheduled tasks in FastAPI to perform background work. With the rapid development of Internet applications, many applications have some background tasks that need to be executed regularly, such as data cleaning, email sending, backup, etc. In order to solve this problem, we can use scheduled tasks to automatically execute background work. In this article, we will introduce how to use scheduled tasks in the FastAPI framework to perform background work. FastAPI is a modern, fast (high-performance) web framework mainly used for building APIs. it has

How to use PHP to develop a scheduled refresh function for web pages How to use PHP to develop a scheduled refresh function for web pages Aug 17, 2023 pm 04:25 PM

How to use PHP to develop a scheduled refresh function for web pages. With the development of the Internet, more and more websites need to update display data in real time. Refreshing the page in real time is a common requirement, which allows users to obtain the latest data without refreshing the entire page. This article will introduce how to use PHP to develop a scheduled refresh function for web pages and provide code examples. The simplest way to implement scheduled refresh using Meta tag is to use HTML Meta tag to refresh the page regularly. In HTML<head>

See all articles