Home > php教程 > php手册 > body text

Execute PHP scripts regularly under Linux (detailed explanation of how to use crontab regularly)

WBOY
Release: 2016-07-09 09:10:32
Original
1251 people have browsed it
Detailed explanation of how to use crontab for scheduled execution
How to use:
crontab [ -u user ] filecrontab [ -u user ] { -l | -r | -e }
Description:
Crontab is used to allow users to execute programs at fixed times or fixed intervals. In other words, it is similar to the user's schedule. -u user refers to setting the schedule of the specified user. The premise is that you must have its permissions (for example, root) to specify other people's schedules. If -u user is not used, it means setting your own schedule.
Parameters:

-e: Execute a text editor to set the schedule. The default text editor is VI. If you want to use another text editor, please set the VISUAL environment variable to specify which text editor to use (for example setenv VISUAL joe)
-r: Delete the current schedule
-l: List the current schedule

The format of the schedule is as follows:
f1 f2 f3 f4 f5 program

Where f1 represents minutes, f2 represents hours, f3 represents the day of a month, f4 represents the month, and f5 represents the day of the week. program represents the program to be executed.
When f1 is *, it means the program will be executed every minute, when f2 is *, it means the program will be executed every hour, and so on
When f1 is a-b, it means that it will be executed from the a-th minute to the b-th minute. When f2 is a-b, it means that it will be executed from the a-th hour to the b-th hour, and so on.
​When f1 is */n, it means that it will be executed every n minutes. When f2 is */n, it means it will be executed every n hours. The rest can be deduced
When f1 is a, b, c,..., it means that the a, b, c,... minutes are to be executed. When f2 is a, b, c,..., it means that the a, b, c... It takes hours to execute, and so on for the rest
Users can also store all settings in a file first and use crontab file to set the schedule.
Example:
Execute /bin/ls:
at the 0th minute of every hour every day of the month 0 7 * * * /bin/ls

Within 12 months, /usr/bin/backup will be executed every 20 minutes from 6 am to 12 am every day:
0 6-12/3 * 12 * /usr/bin/backup

​Send a letter to admin
@domain.name:
every day from Monday to Friday at 5:00 pm 0 17 * * 1-5 mail -s "hi"
admin@domain.name
​Execute echo "haha"
at midnight every day of the month at 0:20, 2:20, 4:20.... 20 0-23/2 * * * echo "haha"

Note:
When the program is executed at the time you specify, the system will send you a letter showing the contents of the program execution. If you do not want to receive such a letter, please add > /dev after each line with a space. /null 2>&1 will do.

There are basically two ways to create routine commands in crontab :

One is for all users, who can issue work schedules through the crontab -e command;
The other is for system administrators. You can directly modify the /etc/crontab file to directly execute it regularly.
If you need to send a letter to yourself at 12:00 noon every day,
#crontab -e
Enter the vi editing screen to edit your routine commands, enter the following statement
0 12 * * * mail xxx@163.com
There are 5 numbers above, which mean:
Points (0-59)
Hours (0-23)
Date (1-31)
Month (1-12)
Week(0-6)
In addition, if it is [*], it means that all numbers are applicable.
So, the above statement is to execute the command mail xxx@163.com
Example 1:
Send an email to your friend. It will be sent at 23:59 on May 1st. Use:
# crontab -e 59 23 1 5 * mail xxx@163.com
Example 2:
To check related files every 6 minutes, use:
# crontab -e */6 * * * * /home/cheney/test.sh

Delete routine commands:
# crontab -r will delete it

cron
In Linux, tasks can be configured to run automatically during a specified time period, on a specified date, or when the average system load falls below a specified amount. Red Hat Enterprise Linux is preconfigured to run critical system tasks so that the system can be kept updated. For example, the slocate database used by the locate command is updated daily. System administrators can use automated tasks to perform scheduled backups, monitor systems, run custom scripts, and more.

Red Hat Enterprise Linux comes with several tools to automate tasks: cron, at, and batch.

Cron is a daemon process that can be used to schedule the execution of recurring tasks based on a combination of time, date, month, and week.

cron assumes the system is running continuously. If the system is not running when a task is scheduled, the task will not be executed.

To use the cron service, you must have the vixie-cron RPM package installed and the crond service must be running. To determine whether the package is installed, use the rpm -q vixie-cron command. To determine whether the service is running, use the /sbin/service crond status command.

Configure cron tasks

The main configuration file of cron is /etc/crontab, which includes the following lines:

<tt class="COMPUTEROUTPUT">SHELL=/bin/bash
            PATH=/sbin:/bin:/usr/sbin:/usr/bin
            MAILTO=root
            HOME=/
            # run-parts
            01 * * * * root run-parts /etc/cron.hourly
            02 4 * * * root run-parts /etc/cron.daily
            22 4 * * 0 root run-parts /etc/cron.weekly
            42 4 1 * * root run-parts /etc/cron.monthly</tt>
Copy after login

The first four lines are variables used to configure the cron task running environment. SHELL The value of the variable tells the system which shell environment to use (in this case, bash shell); PATH The variable definition is used The path to execute the command. The output of the cron job is mailed to the username defined by the MAILTO variable. If the MAILTO variable is defined as a blank string (MAILTO=""), the email will not be sent. The HOME variable can be used to set the home directory used when executing commands or scripts.

/etc/crontab Each line in the file represents a task, and its format is:

<tt class="COMPUTEROUTPUT">minute   hour   day   month   dayofweek   command</tt>
Copy after login

 

  • minute — 分钟,从 0 到 59 之间的任何整数

  • hour — 小时,从 0 到 23 之间的任何整数

  • day — 日期,从 1 到 31 之间的任何整数(如果指定了月份,必须是该月份的有效日期)

  • month — 月份,从 1 到 12 之间的任何整数(或使用月份的英文简写如 jan、feb 等等)

  • dayofweek — 星期,从 0 到 7 之间的任何整数,这里的 0 或 7 代表星期日(或使用星期的英文简写如 sun、mon 等等)

  • command — 要执行的命令(命令可以是 ls /proc >> /tmp/proc 之类的命令,也可以是执行你自行编写的脚本的命令。)

在以上任何值中,星号(*)可以用来代表所有有效的值。譬如,月份值中的星号意味着在满足其它制约条件后每月都执行该命令。

整数间的短线(-)指定一个整数范围。譬如,1-4 意味着整数 1、2、3、4。

用逗号(,)隔开的一系列值指定一个列表。譬如,3, 4, 6, 8 标明这四个指定的整数。

正斜线(/)可以用来指定间隔频率。在范围后加上 /<integer> 意味着在范围内可以跳过 integer。譬如,0-59/2 可以用来在分钟字段定义每两分钟。间隔频率值还可以和星号一起使用。例如,*/3 的值可以用在月份字段中表示每三个月运行一次任务。

开头为井号(#)的行是注释,不会被处理。

如你在 /etc/crontab 文件中所见,它使用 run-parts 脚本来执行 /etc/cron.hourly/etc/cron.daily/etc/cron.weekly/etc/cron.monthly 目录中的脚本,这些脚本被相应地每小时、每日、每周、或每月执行。这些目录中的文件应该是 shell 脚本。

如果某 cron 任务需要根据调度来执行,而不是每小时、每日、每周、或每月地执行,它可以被添加到 /etc/cron.d 目录中。该目录中的所有文件使用和 /etc/crontab 中一样的语法。

<tt class="COMPUTEROUTPUT"># record the memory usage of the system every monday
            # at 3:30AM in the file /tmp/meminfo
            30 3 * * mon cat /proc/meminfo >> /tmp/meminfo
            # run custom script the first day of every month at 4:10AM
            10 4 1 * * /root/scripts/backup.sh</tt>
Copy after login

Example 37-1. crontab example

Users other than root can use the crontab tool to configure cron tasks. All user-defined crontabs are saved in the /var/spool/cron directory and executed using the identity of the user who created them. To create a crontab project as a user, log in as that user and type the crontab -e command using either the VISUAL or EDITOR The editor specified by the environment variable to edit the user's crontab. This file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab file is saved according to the username and written to the file /var/spool/cron/username in.

The

cron daemon checks every minute the /etc/crontab file, the etc/cron.d/ directory, and the directory. 🎜>/var/spool/cron

Changes in the directory. If changes are found, they are loaded into memory. This way, you don't have to restart the daemon when a crontab file changes.

Control the use of cron

The /etc/cron.allow and /etc/cron.deny files are used to restrict the use of cron. Both formats using control files are one user per line. No spaces are allowed in both files. The cron daemon (crond

) does not have to be restarted if the usage control file is modified. Use a control file that is read every time a user adds or removes a cron task.

Root can always use cron regardless of what is specified in the usage control file.

If the cron.allow file exists, only users listed in it are allowed to use cron, and cron.deny

The file will be ignored.

If the cron.allow file does not exist, all users listed in cron.deny

are banned from using cron .

Start and stop services

To start the cron service, use the /sbin/service crond start command. To stop the service, use the /sbin/service crond stop

command. It is recommended that you start this service at boot time.
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!