Home Backend Development PHP Tutorial Detailed explanation of scheduled task crontab in PHP

Detailed explanation of scheduled task crontab in PHP

Jul 28, 2016 am 08:27 AM
cron crontab etc

Recently I have encountered a lot of questions about scheduled tasks. To be honest, the PHP script itself has one or two functions that can be combined to create scheduled tasks, but the effect is very average. The first choice is to plan the system tasks better, whether it is win or Linux system. Task planning function, and what we have to do is to make good use of these functions. The following is a detailed explanation of scheduled tasks in Linux. Because it is too long, I directly found a copy. If you need it, you can take a look. We just use this to execute our tasks regularly. Let’s not talk too much about the specific PHP files~
cron is a scheduled execution tool under Linux that can run jobs without manual intervention. Since Cron is a built-in service of Linux, but it does not start automatically, you can use the following methods to start and shut down the service:
/sbin/service crond start //Start the service
/sbin/service crond stop //Close the service
/sbin /service crond restart //Restart the service
/sbin/service crond reload //Reload the configuration
You can also start this service automatically when the system starts:
In the /etc/rc.d/rc.local script Add at the end:
/sbin/service crond start
Now that the Cron service is already in the process, we can use this service. The Cron service provides the following interfaces for everyone to use:
1. Edit directly with the crontab command
The cron service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:
crontab -u //Set the cron service of a certain user. Generally, the root user needs this parameter when executing this command.
crontab -l //List the details of a certain user's cron service
crontab -r //Delete a certain user's cron service
crontab -e //Edit a certain user's cron service
For example, root can view his own cron Settings: crontab -u root -l
For another example, root wants to delete fred's cron settings: crontab -u fred -r
When editing the cron service, the edited content has some formats and conventions, enter: crontab -u root -e
Enter vi editing mode. The edited content must conform to the following format: */1 * * * * ls >> /tmp/ls.txt
The first part of this format is to set the time, and the latter part is If there are too many commands to be executed, you can write these commands into a script and then call the script directly here. Remember to write the full path of the command when calling. We have a certain agreement on setting the time. The first five * signs represent five numbers. The value range and meaning of the numbers are as follows:
minutes (0-59)
hours (0-23)
date (1-31)
Month (1-12)
Week (0-6) //0 represents Sunday
In addition to numbers, there are several special symbols: '*', '/' and '-', ',', * represents everything Numbers within the value range, '/' means every, '*/5' means every 5 units, '-' means from a certain number to a certain number, ',' separates several discrete numbers. Here are a few examples to illustrate the problem:
Every morning at 6 o'clock
0 6 * * * echo 'Good morning.' >> /tmp/test.txt //Note that with simple echo, no output can be seen on the screen. Because cron emails any output to the root mailbox.
Every two hours
0 */2 * * * echo 'Have a break now.' >> /tmp/test.txt
Every two hours between 11pm and 8am, 8am
0 23-7/2, 8 * * * echo 'Have a good dream:)' >> /tmp/test.txt
On the 4th of every month and at 11 a.m. from Monday to Wednesday every week Click
0 11 4 * 1-3 command line
At 4 a.m. on January 1st
0 4 1 1 * command line
Every time after editing a user's cron settings, cron will automatically be under /var/spool/cron Generate a file with the same name as this user. The cron information of this user is recorded in this file. This file cannot be edited directly and can only be edited with crontab -e. After cron starts, it reads this file every time and checks whether the commands in it need to be executed. Therefore, there is no need to restart the cron service after modifying this file.
2. Edit the /etc/crontab file to configure cron
The cron service not only needs to read all the files in /var/spool/cron once every minute, but also needs to read /etc/crontab once, so we can configure this file to use the cron service Do something. Configuration with crontab is for a certain user, while editing /etc/crontab is a task for the system. The file format of this file is:
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root //If an error occurs or there is data output, the data will be sent as an email Send to this account
HOME=/ //The path where the user runs, this is the root directory
# run-parts
01 * * * * root run-parts /etc/cron.hourly //Execute /etc/cron every hour Scripts in .hourly
02 4 * * * root run-parts /etc/cron.daily //Execute scripts in /etc/cron.daily every day
22 4 * * 0 root run-parts /etc/cron.weekly //Execute the scripts in /etc/cron.weekly every week
42 4 1 * * root run-parts /etc/cron.monthly //Execute the scripts in /etc/cron.monthly every month
Please pay attention to 'run -parts' parameter. If you remove this parameter, you can later write the name of a script to be run instead of the folder name.
-------------------------------------------------
Basic format:
*  * * *  * command
Time-sharing, day, month, and week commands
The first column represents minutes 1 to 59. Each minute is represented by * or */1
The second column represents hours 1 to 23 (0 represents 0 o'clock)
The third column represents dates 1 to 31
Column 4 represents the month 1 to 12
Column 5 identifies the day of the week 0 to 6 (0 means Sunday)
Column 6 represents the command to be run
Some examples of crontab files:
30 21 * * * /usr/local/etc /rc.d/lighttpd restart
The above example indicates that lighttpd is restarted at 21:30 every night.
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
The above example indicates that lighttpd is restarted at 4:45 on the 1st, 10th, and 22nd of every month.
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
The above example indicates that lighttpd is restarted at 1:10 every Saturday and Sunday.
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
The above example means restarting lighttpd every 30 minutes between 18:00 and 23:00 every day.
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
The above example indicates that lighttpd is restarted every Saturday at 11:00 pm.
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd every hour
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd every hour between 11pm and 7am
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
On the 4th of every month and every Monday to Wednesday Restart lighttpd at 11 o'clock
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
Restart lighttpd at 4 o'clock on January 1st

The above has introduced a detailed explanation of the scheduled task crontab in PHP, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

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

How to read linux crontab error log How to read linux crontab error log Mar 07, 2023 am 09:29 AM

How to view the crontab error log in Linux: 1. View the file directory "/var/log/cron"; 2. Use the "tail -f /var/log/cron" command to view the tail of the file in real time; 3. Use "vim /var /log/cron" command can be viewed through an advanced text viewer.

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

How to set the scheduled shutdown command in Linux How to set the scheduled shutdown command in Linux Feb 18, 2024 pm 11:55 PM

What is the Linux scheduled shutdown command? When using a Linux system, we often need to schedule a shutdown, such as automatically shutting down after downloading a large number of files, or automatically shutting down the server when it is no longer in use. In Linux systems, scheduled shutdown can be implemented using the "shutdown" command. The "shutdown" command allows the user to shut down or restart the system and set a delay time. By adding parameters to the command, you can implement the scheduled shutdown function. The basic format of the command is as follows: shutdown

How to implement automatic inspection of python apscheduler cron scheduled task trigger interface How to implement automatic inspection of python apscheduler cron scheduled task trigger interface May 01, 2023 am 10:40 AM

There are several types of scheduled task triggering methods for pythoncron scheduled task triggering interface. In daily work, R&D students often use the cron method. I checked that the APScheduler framework supports multiple scheduled task methods. First, install the apscheduler module $pipinstallapscheduler The code is as follows: (The definitions and ranges of various time parameters are annotated in the method) fromapscheduler.schedulers.blockingimportBlockingSchedulerclassTiming:def__init__(self,start_d

How to automatically restart applications in Linux using Systemd and Crontab How to automatically restart applications in Linux using Systemd and Crontab Sep 28, 2023 pm 03:35 PM

How to use Systemd and Crontab to automatically restart applications in Linux systems. In Linux systems, Systemd and Crontab are two very important tools. Systemd is a system and service manager, while Crontab is a tool for automating tasks at specified times. This article will use a specific example to introduce how to use Systemd and Crontab to automatically restart applications in Linux systems. Suppose we have a No

How to let all users print specified prompt information when logging in in Linux How to let all users print specified prompt information when logging in in Linux Feb 19, 2024 pm 05:12 PM

In Linux systems, through configuration files and scripts, you can display specified prompt information when all users log in. Next, we will introduce several commonly used implementation methods. Method 1: Modify the /etc/issue file. Open the terminal and use a text editor (such as vi or nano) to edit the /etc/issue file with root permissions. sudovi/etc/issue Add the prompt message you want to display at the end of the file, for example: Welcome to MyLinuxSystem! Please beaware that all activities are monitored. Save and close the file. Now when the user logs in, the system will display /

See all articles