In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces what the basic command of Linux timing task refers to, the content is very detailed, interested friends can refer to, hope to be helpful to you.
In the process of using a computer, there are often some planned tasks that need to be executed at some point in the future. Linux provides some methods to set scheduled tasks.
1 、 at
The command at reads the command from a file or standard input and executes it at a future time, only once. The normal execution of at requires a daemon atd:
# install at yum install-y at or apt-get install at-y # start the daemon process service atd start or systemctl start atd # check whether to boot (see this article for systemctl) chkconfig-- list | grep atd or systemctl list-unit-files | grep atd # set boot chkconfig-- level 235 atd on or systemctl enable atd
If you do not use pipe | or specify the option-f, the execution of at will be interactive, requiring you to enter a command at the at prompt:
[root@centos7 temp] # at now + 2 minutes # execute at and specify the next two minutes of the current time at > echo hello world > / root/temp/file # manually enter the command and enter at > # ctrl+d end input job 9 at Thu Dec 22 14:05:00 2016 # display the task number and execution time [root@centos7 temp] #
Option-l or command atq query task
[root@centos7 temp] # atq 9 Thu Dec 22 14:05:00 2016 a root
The task is executed after the arrival time, generating a new file file and saving the output of echo
[root@centos7 temp] # ls-l file-rw-r--r-- 1 root root 12 December 22 14:05 file [root@centos7 temp] # cat file hello world [root@centos7 temp] #
At has a variety of ways to specify time, which can be
1) hh:mm hours: minutes (for the current day, if the time has passed, it will be executed the next day)
2) midnight (late at night), noon (noon), teatime (afternoon tea time, 4 p.m.), today,tomorrow, etc.
3) 12-hour timing, followed by am (morning) or pm (afternoon)
4) specify the specific execution date mm/dd/yy (month / day / year) or dd.mm.yy (day. Month. Year)
5) relative timing now + n units,now is the present moment, n is the number, and units is the unit (minutes, hours, days, weeks)
For example, create a directory at 02:20 tomorrow afternoon
[root@centos7 temp] # at 02:20pm tomorrow at > mkdir / root/temp/X at > job 11 at Fri Dec 23 14:20:00 2016
The option-d or the command atrm indicates to delete the task
[root@centos7 temp] # at-d 11 # Delete Task 11 (example above) [root@centos7 temp] # atq [root@centos7 temp] #
You can use pipes | or option-f to let at get tasks from standard input or files
[root@centos7 temp] # cat test.txt echo hello world > / root/temp/file [root@centos7 temp] # at-f test.txt 5pm + 2 days job 12 at Sat Dec 24 17:00:00 2016 [root@centos7 temp] # cat test.txt | at 16:20 12-23-16 job 13 at Fri Dec 23 16:20:00 2016
Atd uses two files / etc/at.allow and / etc/at.deny to determine which users in the system can use at to set scheduled tasks. It first checks / etc/at.allow, and if the file exists, only the users listed in the file (one user name per line) can use at;. If it does not exist, check the file / etc/at.deny, and all users who are not in this file can use at. If / etc/at.deny is an empty file, it means that all users in the system can use at;. If the / etc/at.deny file does not exist, only superusers (root) can use at.
2 、 crontab
The command crontab is used to set, remove, and list the service crond table. The function of the crond service is similar to atd, except that crond can set the task to be executed multiple times. It is relatively more common than atd.
You also need to start the service crond
[root@centos7 temp] # ps-ef | grep [c] rond root 733 December 20? 00:00:00 / usr/sbin/crond-n
Each user in the system can have their own cron table, similar to atd, crond also has two files / etc/cron.allow and / etc/cron.deny to restrict users from using cron, and the rules are the same as those of atd.
Option-l to list the cron items for the current user
Option-u indicates the specified user
[root@centos7 ~] # crontab-l-u learner no crontab for learner [root@centos7 ~] #
The option-e indicates the cron table of the editing user. The default editor is selected when editing, which is vi in the author's environment.
You can set the system-level cron table by editing the file / etc/crontab directly.
When editing using crontab-e, a temporary file is generated under / tmp. After saving, crond will write the contents to a file with the same name as the user name under / var/spool/cron, and crond will check the syntax when saving. This is also the recommended use of setting up scheduled tasks.
Syntax:
* command
Each line represents a task, and a line that begins with the symbol # indicates a comment and does not take effect. Each effective row is shaped like the one shown above, and the line is divided into six parts, where:
* * part represents minutes (0-59), * indicates hours per minute (0-23), * indicates days (1-31), * indicates months (1-12), * indicates days of the week (0-6), and * indicates tasks to be performed every day of the week.
In the first five parts of the time setting, in addition to * representing any time in the current part, three other symbols /,-denote every interval, time point An and time point B, time point A to time point B are supported.
For example, test the connectivity of 10.0.1.252 every 3 minutes and append the results to / root/252.log
[root@centos7] # crontab-e * / 3 * / usr/bin/ping-c1 10.0.1.252 & > > / root/252.log
After saving, the word crontab: installing new crontab will appear. Note that none of the six parts can be empty. The command * * writes an absolute path. When editing regular tasks of ordinary users, you should pay attention to the execution permissions of the command.
For example, from January to May, backup tasks are performed at 2:30 every Tuesday and Friday.
30 2 * 1-5 2 bin/bash 5 / root/temp/backup.sh
Here the backup task is written to the script / root/temp/backup.sh for execution
Such as March-June and September-December, from 12:00 to 14:00 every Monday to Friday, performing refresh tasks every 2 minutes
* / 2 12-14 * 3-6 bin/bash 9-12 1-5 / root/temp/refresh.sh
By using a mixture of date time and special symbols, you can combine most of the desired times.
View scheduled tasks
[root@centos7] # crontab-l * / 3 * / usr/bin/ping-C1 10.0.1.252 & > > / root/252.log 30 2 * 1-52, 5 / bin/bash / root/temp/backup.sh * / 2 12-14 * 3-6 bin/bash 9-121-5 / bin/bash / root/temp/refresh.sh
Option-r means to delete a scheduled task
[root@centos7 ~] # crontab-r [root@centos7 ~] # crontab-l no crontab for root
One of the problems often encountered when using crontab is that commands or scripts that can be executed normally on the command line cannot be executed properly when scheduled tasks are set. The reason for this is generally because crond sets a different environment variable for a command or script than logging in to shell
[root@centos7] # head-3 / etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root [root@centos7 ~] # [root@centos7 ~] # echo $PATH / usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin [root@centos7 ~] #
Here the values in the PATH of crond are different from those in shell, and the PATH environment variable defines the path to search for commands when shell executes commands. More about environment variables will be explained in more detail in the shell programming article.
For system-level scheduled tasks, these tasks are even more important. Most linux systems contain a series of cron-related subdirectories in / etc: / etc/cron. {hourly,daily,weekly,monthly}, the files in the directory define the scripts that need to be run every hour, daily, weekly, and monthly, and the exact time to run these tasks is specified in the file / etc/crontab. Such as:
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
For servers that are powered on 24 hours a day, the regular operation of these tasks ensures the stability of the server. However, it is noted that these tasks are usually performed in the early hours of the morning, and for linux computers (such as laptops) that often need to be shut down, they are likely to be shut down when they need to run cron, cron can not be run, and a long time will cause the system to slow down. For such a system, linux introduces another tool, anacron, to perform system timing tasks.
The purpose of anacron is not to completely replace cron, but as a supplement to cron. The task for anacron is defined in the file / etc/anacrontab:
# / etc/anacrontab: configuration file for anacron # See anacron (8) and anacrontab (5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 # period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts / etc/cron.daily 7 25 cron.weekly nice run-parts / etc / cron.weekly @ monthly 45 cron.monthly nice run-parts / etc/cron.monthly
Unlike cron, which runs as a daemon, anacron runs and terminates as a normal process. For each defined task, anacron will check the task that should be run after the system starts to determine whether the last run has exceeded the predetermined number of days (* column of the task row in / etc/anacrontab), and if it is greater than the predetermined number of days, it will delay running the task after one time (the second column of the task row in / etc/anacrontab). This ensures the execution of the mission. For more information about anacron, please refer to the relevant documentation.
3 、 systemd.timer
Crond and atd services are minute-based, meaning they wake up every minute to check to see if there are any tasks to perform. If the execution of a task needs to be accurate to seconds, crond and atd are powerless. On systemd-based systems, scheduled tasks that are accurate to seconds can be achieved through the timer systemd.timer.
We mentioned the concept of service units in systemd in the previous article, and we need to use two of them here: .service and .timer. The .service is responsible for configuring the tasks to be run, and the .timer is responsible for configuring the execution time.
Let's first look at an example:
Create a task script
[root@centos7 temp] # cat / root/temp/ping252.sh #! / bin/bash ping-C1 10.0.1.252 & > > / root/temp/252.log
Configure service .service
[root@centos7 temp] # cd / usr/lib/systemd/system [root@centos7 system] # cat ping252.service [Unit] Description=ping 252 [Service] Type=simple ExecStart=/root/temp/ping252.sh [root@centos7 system] #
Configure timer .timer
[root@centos7 temp] # cd / usr/lib/systemd/system [root@centos7 system] # cat ping252.timer [Unit] Description=ping 252 every 30s [Timer] # Time to wait after enable this unit OnActiveSec=60 # Time between running each consecutive time OnUnitActiveSec=30 Unit=ping252.service [Install] WantedBy=multi-user.target [root@centos7 system] #
Enable timer
[root@centos7 system] # systemctl enable ping252.timer Created symlink from / etc/systemd/system/multi-user.target.wants/ping252.timer to / usr/lib/systemd/system/ping252.timer. [root@centos7 system] # systemctl start ping252.timer
View
# timer [root@centos7 system] # systemctl status ping252.timer ● ping252.timer-ping252 every 30s Loaded: loaded (/ usr/lib/systemd/system/ping252.timer; enabled; vendor preset: disabled) Active: active (waiting) since five 2016-12-23 14:27:26 CST; 3min 42s ago December 23 14:27:26 centos7 systemd [1]: Started ping252 every 30s. December 23 14:27:26 centos7 systemd [1]: Starting ping 252 every 30s. # Service [root@centos7 system] # systemctl status ping252 ● ping252.service-ping252 Loaded: loaded (/ usr/lib/systemd/system/ping252.service; static; vendor preset: disabled) Active: active (running) since five 2016-12-23 14:35:38 CST; 2ms ago Main PID: 11494 (ping252.sh) CGroup: / system.slice/ping252.service └─ 11494 / bin/bash / root/temp/ping252.sh 23 December 14:35:38 centos7 systemd [1]: Started ping252. December 23 14:35:38 centos7 systemd [1]: Starting ping 252.
Deactivate
[root@centos7 system] # systemctl disable ping252.timer Removed symlink / etc/systemd/system/multi-user.target.wants/ping252.timer. [root@centos7 system] # systemctl stop ping252.timer [root@centos7 system] #
The timer is enabled for 1 minute to see the generation of the / root/temp/252.log file, and then the content is written every 30 seconds. The service unit configuration file of systemd is separated into different configuration blocks by different tags, where:
The [Unit] tag specifies general configuration information that does not depend on a specific type, for example, both files in the example specify an option Description= to represent description information.
The installation information of this unit is saved under the [Install] tag, where WantedBy= indicates that when the unit is enabled with the systemctl enable command, a corresponding symbolic link will be created under the specified target's .targets / or .links / (such as the example above). The result is that this unit will also be started when the specified target is started.
In addition to these two tags that can be set for all configuration files (the remaining options can be viewed with the command man 5 systemd.unit), each service unit also has a tag for a specific unit type, such as [Service] in the .service file and [Timer] in the .timer file in our example.
The value after Type= under the [Service] tag indicates the execution mode. Set it to simple and work with ExecStart= to indicate that the specified program (script in our example) will not start with fork (). If set to oneshot indicates that it will only be executed once (similar to at), if you need to let systemd still think the service is active after the service process exits, you also need to set RemainAfterExit=yes. Please use the command man 5 systemd.service to view the remaining options.
A variety of monotonous timers can be specified in the [Timer] tag. The so-called "monotonous time" means that the time increases monotonously and evenly from the boot moment (zero) as long as the system is running (but it remains the same while the system is dormant), never goes back, and has nothing to do with the time zone. Even if the user modifies the system time forward / backward while the system is running, it will not have any effect on the "monotonous time". These include:
OnActiveSec= means relative to the point in time that this unit is enabled OnBootSec= means relative to the point in time when the machine is started, OnUnitActiveSec= means relative to the point in time when systemd is started, relative to the matching unit (the unit specified by Unit= under this label) * the point in time OnUnitInactiveSec= is started once is relative to the matching unit (the unit specified by Unit= under this label) * *
In our example, we use two of the OnActiveSec=60 and OnUnitActiveSec=30 to specify the unit after this unit calls Unit= 60 seconds after it is enabled, and enable it again every 30 seconds after this unit is enabled, achieving the purpose of periodic execution.
The time units specified after these timers can be: us (microseconds), ms (milliseconds), s (seconds), m (minutes), h (hours), d (days), w (weeks), month (months), y (years). If the unit is omitted, the default unit 'seconds' is used. It can be written as 5h 30min for the next 5 hours and 30 minutes.
[Timer] Calendar timer based on Wall clock time (wall clock) can also be set under the OnCalendar=, tag. The so-called "wall clock time" refers to the time of the wall clock in the real world, which in fact is the system time in the operating system, which is read by the operating system from the clock chip on the motherboard at startup. Since this time can be changed manually, it is not necessarily monotonous or uniformly increasing. The time format can be:
Thu,Fri 2012 May 1 May 5 11:12:13 # means the 1st and 5th of any month in 2012, if it is Thursday or Friday Then execute *-*-*: 00 # at 11:12:13 means every minute *-*-* 00:00:00 # means every day *-01 00:00:00 # means every half a year *: 0Come 15 # means every 15 minutes, 14 minutes, 13 minutes, 20 minutes, 10 minutes, 20 minutes, 30 points Mon Fri *-01Charpy 2-01Jol 03 *: 30:45 # represents the 1st and 3rd of odd-numbered months in any year If it is Monday or Friday, it will be executed at 30 minutes and 45 seconds per hour.
Other contents of monotone timers and calendar timers can be queried by using the command man 7 systemd.time
The Unit= then indicates the service unit associated with this timer (ping252.service in our example).
Most of the setting options in the service unit allow you to specify multiple times, which will take effect if there is no conflict. For example, multiple Unit can be set in .timer to indicate that these service units share a timer.
In addition, the option Persistent=, can be set under the [Timer] tag, which is only meaningful for calendar timers defined by the OnCalendar= instruction. If set to yes (default is no), the last trigger time of the matching unit * is saved on disk. In this way, when the timer unit is started again, if the matching unit should have been started at least once during the timer unit stop, the matching unit will be started immediately. In this way, you won't miss the tasks you have to perform because of the shutdown. (similar to anacron)
More options on timers can be seen at man systemd.timer
Using systemd.timer to set up scheduled tasks can replace all the functions of atd and crond, and systemd takes over many other services.
On the Linux scheduled tasks of the basic command refers to what is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
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.