您的位置:首页 > 运维架构 > Linux

No.3 Linux计划任务

2018-01-31 21:10 274 查看
一. 简介
作用: 计划任务主要是做一些周期性的任务,目前最主要的用途是定期备份数据

Schedule one-time tasks with at. 一次性调度执行 at
Schedule recurring jobs with cron. 循环调度执行 cron
Schedule recurring system jobs.
所有计划任务执行中的输出都会以邮件的方式发送给指定用户, 除非重定向
(1)一次性调度执行 at
1)程序安装
在软件所在目录
rpm -ivh at.x86_64 //安装
systemctl start atd //启动
systemctl status atd //查看
Active: active (running) //运行。inactive是不活跃就是没有运行。
2 )语法格式

at <TIMESPEC>
now +5min
teatime tomorrow (teatime is 16:00)
noon +4 days
5pm august 3 2018
(3)循环调度执行 cron
1 )简介
# systemctl status crond.service
# ps aux |grep crond
root 550 0.0 0.0 126300 1648 ? Ss 10:05 0:00 /usr/sbin/crond -n
特点
:crond进程每分钟会处理一次计划任务
2   )语法
** 用户级**:
格式:
语法格式 Job format:

Minutes Hours Day-of-Month Month Day-of-Week Command

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

| | | | |

* command

0 2 /mysql_back.sh
0 2 14 /mysql_back.sh
0 2 14 2 /mysql_back.sh
0 2 5 /mysql_back.sh //不写月日,仅周生效
0 2 6 5 /mysql_back.sh
0 2 2 5 /mysql_back.sh
0 2 2 6 5 /mysql_back.sh //书写月和日,月或日,月日周均生效

/5 /mysql_back.sh
0 2 1,4,6 /mysql_back.sh
0 2 5-9 * /mysql_back.sh

/mysql_back.sh
0 /mysql_back.sh

2 /mysql_back.sh

示例:
00 02 * * * ls //每天2:00整
00 02 1 * * ls //每月1号2:00整
00 02 14 2 * ls //每年2月14号2:00整
00 02 * * 7 ls //每周日2:00整
00 02 * 6 5 ls //每年6月的周五2:00整  (特殊)
00 02 14 * 7 ls //每月14号2:00整 或者 每周日2:00整,这两个时间都执行
00 02 14 2 7 ls //每年2月14号2:00整 或者 每周日2:00整,这两个时间都执行

00 02 * * * ls //每天2:00整
* 02 * * * ls //每天2:00中的每一分钟
* * * * * ls //每分钟执行ls
* * 14 2 * ls //2月14号的每分钟 1440分钟

*/5 * * * * ls //每隔5分钟
00 02 1,5,8 * * ls //每月1,5,8号的2:00整
00 02 1-8 * * ls //每月1到8号的2:00整

**系统级**
1)简介
系统级任务计划作用:

1.临时文件的清理 /tmp /var/tmp
2.系统信息的采集 sar
3.日志的轮转(切割)logrotate
4.通常不是由用户定义
2)定义位置一:

vim /etc/crontab //该文件中默认没有定义任何计划任务

* * * * * user-name command to be executed
定义位置二
简介:
以天为单位或者是在启动后立刻进行 anacron 的动作,侦测停机期间应该进行但是并没有进行的 crontab        任务,并运行一遍后自动停止。
运转周期:
时,天,周,月
工作原理:
anacron 会以一小时,一天、七天、一个月为期去侦测系统未进行的 crontab 任务,因此对于某些特殊的使用环境(关机未执行)非常有帮助。
举例说明:
周末关机了。周一早上开机,anacron就会检查一下没有执行的任务计划,然后关闭。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Linux 计划 任务