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--
This article focuses on "how to implement planned tasks under CentOS". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to carry out planned tasks under CentOS".
1. Scheduled tasks executed in a loop
Under linux, there are two scheduled tasks, atd and crond, in which the at command used by the atd service can only be executed once, while the crontab-defined command used by the crond service is circular, so crond meets our requirements.
Crontab supports two states: first, write planned tasks directly; second, the way you use the directory, those placed in the directory will be executed regularly.
2. Users who can be used
By default, users who can log in to the system can use crontab to define scheduled tasks. However, permissions can be restricted through the following two files:
Quote
◆ / etc/cron.allow:
Write an account that can use crontab, but users who are not in this file cannot use crontab
◆ / etc/cron.deny:
Write accounts that cannot use crontab, and users who are not recorded in this file can use crontab.
The definition is similar to that defined by TCPWrapper.
3. Directly use crontab to edit scheduled tasks:
Command:
Crontab [- u username] [- l |-e |-r]
Parameters:
-u: help other users create / remove crontab through-u
-e: edit the contents of crontab
-l: view the contents of crontab
-r: remove all the contents of crontab (all of them. If you just delete one, you can edit it with-e)
Content format:
* Command
The first five * signs indicate minutes, hours, days, months and weeks, such as:
Meaning minute, hour, date, month, week
Number range 0-59 0-23 1-31 1-12 0-7
The * symbol represents the meaning accepted at any time, arbitrary.
The * signs are separated by a space, if it is a range, connect with a-sign; if it is a few times apart, it is indicated by a sign.
In addition, the command must be executed by the user who wrote the scheduled task and end up with an absolute path.
For example:
# crontab-e
59 23 1 5 * mail linuxing < / home/test.txt
Send the content of / home/test.txt to linuxing users as an email at 23:59 on May 1st.
* / 5 * / opt/test.sh
Execute the / opt/test.sh script every 5 minutes
0 3jue 6 * / usr/local/bin/test.sh
Execute the / usr/local/bin/test.sh command at 3: 00 and 6: 00 on the hour
0 8-12 * / root/backup.sh
/ root/backup.sh every hour between 8: 00 and 12:00
4. Perform scheduled tasks in a directory-based manner
The scheduled tasks of the system have been defined in / etc/crontab, using a directory-based approach. The system reads the file regularly and executes the command according to the definition in it.
You can write / etc/crontab files directly using vi in the following format:
# cat / etc/crontab
Shell used by SHELL=/bin/bash #
PATH=/sbin:/bin:/usr/sbin:/usr/bin # predefined PATH path
MAILTO=root # sends Email to this user when there is a problem.
HOME=/ # Home Directory
# run-parts
01 * root run-parts / etc/cron.hourly # hourly directory
02 4 * root run-parts / etc/cron.daily # every day
22 4 * 0 root run-parts / etc/cron.weekly # every Sunday
42 4 1 * * root run-parts / etc/cron.monthly # 1st of each month
Time-sharing, day, month and week executor identity order
You can see that the definition of the first five parameters is the same as editing the scheduled task directly, with the addition of user-defined and run-parts parameters for execution.
Run-parts is followed by the directory name, for example: / etc/cron.hourly, which means that all executables in the / etc/cron.hourly directory are executed every 01 minutes an hour; of course, the directory can be defined by itself.
If you need to increase the system's scheduled tasks, just add the execution file in the corresponding directory, for example: I need to perform the updatedb operation every day, then I just need to link the / usr/bin/updatedb execution command to the / etc/cron.daily directory.
"similarly, if you do not need to use directories, you can also use the following ways:
02 01 * root/ root/test.sh
That is, without run-parts, it is followed directly by the absolute path of the command.
5. Points for attention
◆ if the scheduled task does not take effect after editing the scheduled task using crontab or modifying the / etc/crontab file directly, you may need to restart the crond service: service crond restart
◆ when writing the / etc/crontab file, don't leave out the user who performs the scheduled task, which is different from editing directly with crontab-e.
The scheduled tasks edited by a ◆ user (such as root) with crontab-e are stored in / var/spool/cron/root, this file. But it's best not to edit him directly, because when crond executes, the temporary files you need will be created in the / tmp directory, and direct editing will have an impact on this, or even make mistakes.
Every work performed by ◆ cron is logged to the log file / var/log/cron, from which you can see the status of the command execution.
At this point, I believe you have a deeper understanding of "how to implement planned tasks under CentOS". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.