Linux explanation of scheduled tasks
Directory of this article:
##12.1 Configuring scheduled tasks
12.2 crontab file
12.3 Debugging of crond command
12.4 Task planning accurate to seconds
12.1 Configuring scheduled tasksThe concepts that need to be understood first:
(1).crond is a daemon class program, the path is /usr/sbin/crond. By default, it will be started in the background mode. When crond is started in service or systemd mode, it will also be started in the background mode by default. (2).crondtab is a tool for managing crontab files, and crontab file is a file that defines scheduled task entries. (3).crontab file exists in many places, including system scheduled task files /etc/crontab and /etc/cron.d/*, as well as task files unique to each user /var/spool/ cron/USERNAME. Then there is the crontab command:-l:列出定时任务条目 -r:删除当前任务列表终端所有任务条目 -i:删除条目时提示是否真的要删除 -e:编辑定时任务文件,实际上编辑的是/var/spool/cron/*文件 -u:操作指定用户的定时任务
* * * * * /bin/echo "the first cron entry" >>/tmp/crond.txt
[root@server2 ~]# cat /etc/crontab SHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | |# * * * * * user-name command to be executed
(8). In the command paragraph (i.e. paragraph 6), the percent sign "%" cannot appear arbitrarily, because it represents the special meaning of line break, and all characters after the first % The string will be treated as the standard input of the command.
For example, the following definition:* * * * * /bin/cat >>/tmp/crond.txt %"the first %%cron entry%"
"the firstcron entry"
* * * * * cp /etc/fstab /tmp/`date +\%Y-\%m-\%d`.txt
Another time period setting that needs attention is that using the * sign causes low-level time to overwrite high-level time. For example, "* */2 * * *", it does not mean to execute the task every two hours, but every minute. Although the hour bit is set to every two hours, the minute bit is set to every minute. , so it still means that the task is executed every minute. In the same way, the setting on the minute digit of "*/5 */2 * * *" overrides the setting on the hour digit, indicating that it is executed every 5 minutes and ignores the setting of the hour digit; "00 */2 */5 * *" Indicates that the task will be executed every two hours on the hour regardless of the day setting.
12.2 crondtab file
crondtab file为任务定义文件。
(1).在此文件中,空行会被忽略,首个非空白字符且以#开头的行为注释行,但#不能出现在行中。
(2).可以在crontab file中设置环境变量,方式为"name=value",等号两边的空格可随意,即"name = value"也是允许的。但value中出现的空格必须使用引号包围。
(3). 默认crond命令启动的时候会初始化所有变量,除了某几个变量会被crond daemon自动设置好,其他所有变量都被设置为空值。自动设置的变量包括SHELL=/bin/sh,以及HOME和LOGNAME(在CentOS上则称为USER),后两者将被默认设置为/etc/passwd中指定的值。其中SHELL和HOME可以被crontab file中自定义的变量覆盖,但LOGNAME不允许覆盖。当然,自行定义的变量也会被加载到内存。
(4).除了LOGNAME/HOME/SHELL变量之外,如果设置了发送邮件,则crond还会寻找MAILTO变量。如果设置了MAILTO,则邮件将发送给此变量指定的地址,如果MAILTO定义的值为空(MAILTO=""),将不发送邮件,其他所有情况邮件都会发送给crontab file的所有者。
(5).在系统定时任务文件/etc/crontab中,默认已定义PATH环境变量和SHELL环境变量,其中PATH=/sbin:/bin:/usr/sbin:/usr/bin。
(6).crond daemon每分钟检测一次crontab file看是否有任务计划条目需要执行。
12.3 crond命令的调试
很多时候写了定时任务却发现没有执行,或者执行失败,但因为crond是后台运行的,有没有任何提示,很难进行排错。但是可以让crond运行在前端并进行调试的。
先说明下任务计划程序crond的默认执行方式。
使用下面三条命令启动的crond都是在后台运行的,且都不依赖于终端。
[root@xuexi ~]# systemctl start crond.service [root@xuexi ~]# service crond start [root@xuexi ~]# crond
但crond是允许接受选项的。
crond [-n] [-P] [-x flags] 选项说明:-n:让crond以前端方式运行,即不依赖于终端。-P:不重设环境变量PATH,而是从父进程中继承。-x:设置调试项,flags是调试方式,比较有用的方式是test和sch,即"-x test"和"-x sch"。 :其中test调试将不会真正的执行,sch调试将可以看到等待时间。具体的见下面的示例。
先看看启动脚本启动crond的方式。
[root@server2 ~]# cat /lib/systemd/system/crond.service [Unit] Description=Command Scheduler After=auditd.service systemd-user-sessions.service time-sync.target [Service]EnvironmentFile=/etc/sysconfig/crond ExecStart=/usr/sbin/crond -n $CRONDARGSExecReload=/bin/kill -HUP $MAINPID KillMode=process [Install] WantedBy=multi-user.target
它的环境配置文件为/etc/sysconfig/crond,该文件中什么也没设置。
[root@server2 ~]# cat /etc/sysconfig/crond # Settings for the CRON daemon. # CRONDARGS= : any extra command-line startup arguments for crond CRONDARGS=
所有它的启动命令为:/usr/sbin/crond -n。但尽管此处加了"-n"选项,crond也不会前端运行,且不会依赖于终端,这是systemctl决定的。
在解释下如何进行调试。以下面的任务条目为例。
[root@server2 ~]# crontab -e* * * * * echo "hello world" >>/tmp/hello.txt
执行crond并带上调试选项test。
[root@server2 ~]# crond -x test debug flags enabled: test [4903] cron started log_it: (CRON 4903) INFO (RANDOM_DELAY will be scaled with factor 8% if used.) log_it: (CRON 4903) INFO (running with inotify support) log_it: (CRON 4903) INFO (@reboot jobs will be run at computer's startup.)log_it: (root 4905) CMD (echo "hello world" >>/tmp/hello.txt )
执行crond并带上调试选项sch。
[root@server2 ~]# crond -x sch debug flags enabled: sch [4829] cron started log_it: (CRON 4829) INFO (RANDOM_DELAY will be scaled with factor 73% if used.) log_it: (CRON 4829) INFO (running with inotify support) [4829] GMToff=28800log_it: (CRON 4829) INFO (@reboot jobs will be run at computer's startup.)[4829] Target time=1497950880, sec-to-wait=38 # 等待crond daemon下一次的检测,所以表示38秒后crond将检测crontab file user [root:0:0:...] cmd="echo "hello world" >>/tmp/hello.txt " [4829] Target time=1497950940, sec-to-wait=60Minute-ly job. Recording time 1497922081log_it: (root 4831) CMD (echo "hello world" >>/tmp/hello.txt )user [root:0:0:...] cmd="echo "hello world" >>/tmp/hello.txt "[4829] Target time=1497951000, sec-to-wait=60Minute-ly job. Recording time 1497922141log_it: (root 4833) CMD (echo "hello world" >>/tmp/hello.txt )
但要注意,在sch调试结果中的等待时间是crond这个daemon的检测时间,所以它表示等待下一次检测的时间,因此除了第一次,之后每次都是60秒,因为默认crond是每分钟检测一次crontab file的。例如,下面是某次的等待结果,在这几次等待检测过程中没有执行任何任务。
[4937] Target time=1497951720, sec-to-wait=18[4937] Target time=1497951780, sec-to-wait=60[4937] Target time=1497951840, sec-to-wait=60
还可以同时带多个调试方式,如:
[root@server2 ~]# crond -x test,schdebug flags enabled: sch test[4914] cron started log_it: (CRON 4914) INFO (RANDOM_DELAY will be scaled with factor 21% if used.) log_it: (CRON 4914) INFO (running with inotify support) [4914] GMToff=28800log_it: (CRON 4914) INFO (@reboot jobs will be run at computer's startup.)[4914] Target time=1497951540, sec-to-wait=9 user [root:0:0:...] cmd="echo "hello world" >>/tmp/hello.txt " [4914] Target time=1497951600, sec-to-wait=60Minute-ly job. Recording time 1497922741log_it: (root 4916) CMD (echo "hello world" >>/tmp/hello.txt )
这样在调试定时任务时间时,也不会真正执行命令。
12.4 精确到秒的任务计划
默认情况下,crond执行的任务只能精确到分钟,无法精确到秒。但通过技巧,也是能实现秒级任务的。
(1).方法一:不太精确的方法
写一个脚本,在脚本中sleep3秒钟的时间,这样能实现每3秒执行一次命令。
[root@xuexi ~]# cat /tmp/a.sh#!/bin/bash # PATH="$PATH:/usr/local/bin:/usr/local/sbin"for ((i=1;i<=20;i++));dols /tmpsleep 3done
[root@xuexi ~]# cat /var/spool/cron/lisi* * * * * /bin/bash /tmp/a.sh
但是这样的方法不是最佳方法,因为执行命令也需要时间,且crond默认会有一个随机延时,随机延时由变量RANDOM_DELAY定义。
(2).方法二:在cron配置文件中写入多条sleep命令和其他命令。
[root@xuexi ~]# cat /var/spool/cron/lisi* * * * * ls /tmp* * * * * sleep 3 && ls /tmp* * * * * sleep 6 && ls /tmp* * * * * sleep 9 && ls /tmp* * * * * sleep 12 && ls /tmp* * * * * sleep 15 && ls /tmp* * * * * sleep 18 && ls /tmp* * * * * sleep 21 && ls /tmp* * * * * sleep 24 && ls /tmp* * * * * sleep 27 && ls /tmp* * * * * sleep 30 && ls /tmp …* * * * * sleep 57 && ls /tmp
这种方式很繁琐,但是更精确。如果定义到每秒级别就得写60行cron记录。
由此能看出,秒级的任务本就不是crond所擅长的。实际上能用到秒级的任务也比较少。
The above is the detailed content of Linux explanation of scheduled tasks. For more information, please follow other related articles on the PHP Chinese website!

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 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



The Terror Corridor is a mission in Goat Simulator 3. How can you complete this mission? Master the detailed clearance methods and corresponding processes, and be able to complete the corresponding challenges of this mission. The following will bring you Goat Simulator. 3 Horror Corridor Guide to learn related information. Goat Simulator 3 Terror Corridor Guide 1. First, players need to go to Silent Hill in the upper left corner of the map. 2. Here you can see a house with RESTSTOP written on the roof. Players need to operate the goat to enter this house. 3. After entering the room, we first go straight forward, and then turn right. There is a door at the end here, and we go in directly from here. 4. After entering, we also need to walk forward first and then turn right. When we reach the door here, the door will be closed. We need to turn back and find it.

Goat Simulator 3 is a game with classic simulation gameplay, allowing players to fully experience the fun of casual action simulation. The game also has many exciting special tasks. Among them, the Goat Simulator 3 Imperial Tomb task requires players to find the bell tower. Some players are not sure how to operate the three clocks at the same time. Here is the guide to the Tomb of the Tomb mission in Goat Simulator 3! The guide to the Tomb of the Tomb mission in Goat Simulator 3 is to ring the bells in order. Detailed step expansion 1. First, players need to open the map and go to Wuqiu Cemetery. 2. Then go up to the bell tower. There will be three bells inside. 3. Then, in order from largest to smallest, follow the familiarity of 222312312. 4. After completing the knocking, you can complete the mission and open the door to get the lightsaber.

To automate tasks and manage multiple systems, mission planning software is a valuable tool in your arsenal, especially as a system administrator. Windows Task Scheduler does the job perfectly, but lately many people have reported operator rejected request errors. This problem exists in all iterations of the operating system, and even though it has been widely reported and covered, there is no effective solution. Keep reading to find out what might actually work for other people! What is the request in Task Scheduler 0x800710e0 that was denied by the operator or administrator? Task Scheduler allows automating various tasks and applications without user input. You can use it to schedule and organize specific applications, configure automatic notifications, help deliver messages, and more. it

Rescue Steve is a unique task in Goat Simulator 3. What exactly needs to be done to complete it? This task is relatively simple, but we need to be careful not to misunderstand the meaning. Here we will bring you the rescue of Steve in Goat Simulator 3 Task strategies can help you better complete related tasks. Goat Simulator 3 Rescue Steve Mission Strategy 1. First come to the hot spring in the lower right corner of the map. 2. After arriving at the hot spring, you can trigger the task of rescuing Steve. 3. Note that there is a man in the hot spring. Although his name is Steve, he is not the target of this mission. 4. Find a fish named Steve in this hot spring and bring it ashore to complete this task.

TikTok, as one of the most popular social media platforms at the moment, has attracted a large number of users to participate. On Douyin, there are many fan group tasks that users can complete to obtain certain rewards and benefits. So where can I find Douyin fan club tasks? 1. Where can I view Douyin fan club tasks? In order to find Douyin fan group tasks, you need to visit Douyin's personal homepage. On the homepage, you will see an option called "Fan Club." Click this option and you can browse the fan groups you have joined and related tasks. In the fan club task column, you will see various types of tasks, such as likes, comments, sharing, forwarding, etc. Each task has corresponding rewards and requirements. Generally speaking, after completing the task, you will receive a certain amount of gold coins or experience points.

How to Pause Task Manager Process Updates in Windows 11 and Windows 10 Press CTRL+Window Key+Delete to open Task Manager. By default, Task Manager will open the Processes window. As you can see here, all the apps are endlessly moving around and it can be hard to point them down when you want to select them. So, press CTRL and hold it, this will pause the task manager. You can still select apps and even scroll down, but you must hold down the CTRL button at all times.

Achieving task universality is a core issue in the research of basic deep learning models, and is also one of the main focuses in the recent direction of large models. However, in the field of time series, various types of analysis tasks vary greatly. There are prediction tasks that require fine-grained modeling and classification tasks that require extracting high-level semantic information. How to build a unified deep basic model to efficiently complete various timing analysis tasks has not yet been established. To this end, a team from the School of Software of Tsinghua University conducted research on the basic issue of timing change modeling and proposed TimesNet, a task-universal timing basic model. The paper was accepted by ICLR 2023. Author list: Wu Haixu*, Hu Tengge*, Liu Yong*, Zhou Hang, Wang Jianmin, Long Mingsheng Link: https://ope

Frozen or unresponsive programs are easy to kill from Task Manager. But Microsoft has recently provided users with the facility to terminate these tasks directly from the taskbar. While the option isn't rolled out to everyone, it's easily available if you have the Windows Insider build. Here's everything you need to enable the End Task button and close tasks from the taskbar. How to Get the End Task Button from the Taskbar to Kill Apps Currently, the option to enable the End Task button for taskbar apps is only available as a developer option for users with Windows Insider builds. However, this may change in an upcoming feature update as it will be rolled out to users globally on the stable version. If you still
