In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about the cron plan task configuration method in Linux. Many people may not know much about it. In order to let everyone know more, Xiaobian summarizes the following contents for everyone. I hope you can gain something according to this article.
1. crontab command options
The code is as follows:
#crontab -u
-u Specify a user
-l List a user's task schedule
-r Delete a user's task
-e Edit a user's task
2. cron file syntax and writing
The crontab -e command can be used to edit the cron file corresponding to the user under/var/spool/cron, or directly modify the/etc/crontab file. The format is as follows:
The code is as follows:
Minute Hour Day Month Week command
minutes hours days months weeks orders
0-59 0-23 1-31 1-12 0-6 command
Each field represents the following:
The code is as follows:
Minute The minute of each hour that the task is performed
Hour The number of hours of the day that the task is performed
Day The day of the month on which the task is performed
Month The month of the year in which the task is performed
DayOfWeek Day of the week on which the task is performed, 0 means Sunday
Command Specifies the program, script, or command to execute
These fields are optional, except for Command, which must be specified. For unspecified fields, fill their positions with "*".
3. Meaning of several special symbols
"*" represents the number in the value range,
"/" stands for "every,"
"-" stands for going from one number to another,
"," separate several discrete numbers
4. About cron configuration file/etc/crontab
The cron master configuration file is/etc/crontab, which contains the following lines:
The code is as follows:
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
The first four lines are variables used to configure the cron task runtime environment:
The value of the SHELL variable specifies the shell environment (here the bash shell by default); the PATH variable defines the program path used to execute the command; the output of the cron task is mailed to the user name defined by the MAILTO variable, and if the MAILTO variable is defined as a blank string (MAILTO=""), the email is not sent; the HOME variable can be used to set the home directory used when executing the command or script.
As shown in the run-parts section of the/etc/crontab file, it uses run-parts scripts to execute scripts that exist in the/etc/cron.hourly,/etc/cron.daily,/etc/cron.weekly, and/etc/cron.monthly directories, which are executed hourly, daily, weekly, or monthly at preset times, respectively. The files in these directories should be shell scripts with executable permissions (chmod +x filename).
If a cron task needs to be executed on a schedule rather than hourly, daily, weekly, or monthly, it can be added to the/etc/cron.d directory. All files in this directory use the same syntax as in/etc/crontab.
After editing cron settings of a certain user, cron automatically generates a file with the same name as this user under/var/spool/cron. The cron information of this user is recorded in this file. This file cannot be edited directly. It can only be edited with crontab -e. Read this file every minute after cron starts, checking to see if the commands in it are to be executed. Therefore, there is no need to restart cron service after this file is modified.
5. Common cron example reference
5 * * * * ls Specifies that the ls command is executed once every 5 minutes of the hour
30 5 * * * ls Specifies that the ls command is executed at 5:30 every day
30 7 8 * * ls Specify 7:30 on the 8th of each month to execute ls command
30 5 8 6 * ls Specify June 8 of each year at 5:30 to execute ls command
30 6 * * 0 ls Specifies that the ls command is executed every Sunday at 6:30 [Note: 0 means Sunday, 1 means Monday, and so on, which can also be expressed in English, sun means Sunday, mon means Monday, etc.]
30 3 10,20 * * ls 3:30 on the 10th and 20th of each month Execute ls command [Note: "," used to connect multiple discrete periods]
25 8-11 * * * ls Execute ls command at 25th minute of 8-11 o'clock every day [Note: "-" is used to connect consecutive periods]
*/15 * * * ls Execute ls command every 15 minutes [i.e. execute ls command on 0th 15th 30th 45th 60th minute of every hour]
30 6 */10 * * ls Run ls every 10 days at 6:30 every month [i.e. 1, 11, 21, 31 days of the month at 6:30]
Every day 7:50 Execute all executable files in the/etc/cron.daily directory as root
50 7 * * * root run-parts /etc/cron.daily [Note: run-parts parameter indicates that all executable files in the following directory are executed
Output to/tmp/cron1.txt at 10, 20, 30 minutes:
10,20,30 * * * echo "Output once in 10th, 20th and 30th minutes" >>/tmp/cron1.txt
Run a program every two hours as user lzw.me:
0 */2 * * * lzw.me /usr/bin/somecommand >> /dev/null 2>&1
6. special note
A, cron configuration file path
#vi /etc/crontab
B. How to restart cron
#/etc/rc.d/init.d/crond restart
Usage: /etc/rc.d/init.d/crond {start|stop|status|reload|restart|condrestart}
C. Don't forget to change the script to executable
chmod +x filename
After reading the above, do you have any further understanding of how cron projects are configured in linux? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.