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.

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 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 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 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.

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 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.

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

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.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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

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

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment