Article Directory
Linux scheduled tasks:
Scheduled tasks are divided into two categories:
- At some future time to perform a task: at batch
- Periodically run a task: crond
at the command
[root@test ~]# at --help
at: invalid option -- '-'
Usage: at [-V] [-q x] [-f file] [-mMlbv] timespec ...
at [-V] [-q x] [-f file] [-mMlbv] -t time
at -c job ...
atq [-V] [-q x]
at [ -rd ] job ...
atrm [-V] job ...
batch
Usage notes: at [options] [time | batch] [job]
Options:
-f: Specifies the executable file
times:
HH:MM [YY-mm-dd]
moon,midnight…
tomorrow
now+#[min,hours,seconds…]
batch command:
Let the system automatically selects the space-time to perform certain tasks
Example:
1.sh CAT
echo "the Hello"
AT +1 minutes now execute -f 1.sh # 1.sh contents of the documents in 1 minute
atq command: you can go to view the current job queue
3 Thu Feb 13 03:16:00 2020 a root
crond service
Linux system is the crond service control; in Linux systems on top of a lot of scheduled tasks
are basically using crond way, so crond service by default boot from the start; in addition, since the user can customize the scheduled tasks, providing crontab command
Task scheduling of Linux into two categories:
system task scheduling
user scheduling
Task scheduling system:
The system periodically work to be performed, such as regular system write data to disk
[root@test ~]# systemctl status crond # 查看crond服务是正常运行的
[root@test ~]# cat /etc/crontab 就是系统任何调度的配置文件
SHELL=/bin/bash # 默认系统调度任务采用的shell程序
PATH=/sbin:/bin:/usr/sbin:/usr/bin # 默认系统调度任务采用的命令路径
MAILTO=root # 默认本地邮件服务用户
# 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
Example: User root to perform tasks: echo "hello"
time representation:
- Particular value
value to the valid range for the given time
- *
To all the range of valid values over a given time, said that "every ..."
Examples 1: root user to perform specified tasks every night 9.30
30 21 * * * root echo "the Hello"
- Discrete values
#, #, # ...
Examples 2: root users to perform tasks in a weekly Friday 20:00
0 20 * * mon,fri root echo “hello”
- Continuous Value
3 Example: zhangsan users perform tasks in each month 1-9 20:00
0 20 1-9 * * zhangsan echo "is also ready to spend chanting"
- On the execution time, the definition of the step length
/ #: Specify step
Example 4: zhangsan performs specified tasks every 5 minutes
* / 5 * * * * zhangsan echo "hi"
* * / 3 * * * # zhangsan echo.sh performed once every 3 hours
User Task Scheduler:
crontab command definitions: each user has a dedicated crontab file: / var / spool / crond / Username
crontab command:
[root@test cron]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]
Common parameters:
-u the User: Specifies the user to perform, the default is root
the -l: list task list
-e: toilet task
-r: remove tasks
-i: with the -r parameter, Interactive
Example:
[root@test cron]# crontab -e # 将计划任务写到指定文件中
[root@test cron]# cat /var/spool/cron/root
* * * * * echo "hello"
Scheduled Tasks scene:
Log: log retain only 7 days (use scheduled tasks to delete the log to 7 days before at 0:00 every day)
* * * * * 7 -delete the Find /var/log/java/*.log -atime | -exec rm -rf {};
Backup: the specified data backup / mydata /
10 * 0 * * -czxf /backup/mydata.zip.tar the tar / mydata /