首页 > 技术文章 > Linux计划任务

huoxc 2019-05-17 13:59 原文


Linux计划任务

类型:
一次性计划任务
周期性计划任务

一次性计划任务

前提: atd服务必须运行

[root@test01 ~]# systemctl status atd
● atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2017-02-17 17:26:29 CST; 3h 57min ago
 Main PID: 1269 (atd)
   CGroup: /system.slice/atd.service
           └─1269 /usr/sbin/atd -f

2月 17 17:26:29 localhost.localdomain systemd[1]: Started Job spooling tools.
2月 17 17:26:29 localhost.localdomain systemd[1]: Starting Job spooling tools...
[root@test01 ~]# 
[root@test01 ~]# 
[root@test01 ~]# systemctl is-active atd.service 
active

制订一次性计划任务

at

[root@localhost ~]# at 15:30
at> poweroff
at>
job 1 at 2016-11-20 15:30

[root@localhost ~]# at now + 1 minute
at> mkdir /tmp/aaaa
at>
job 2 at 2016-11-20 13:16

周期性计划任务

前提: crond服务必须运行

[root@test01 ~]# systemctl status crond
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 五 2017-02-17 17:26:29 CST; 4h 1min ago
 Main PID: 1267 (crond)
   CGroup: /system.slice/crond.service
           └─1267 /usr/sbin/crond -n

2月 17 17:26:29 localhost.localdomain systemd[1]: Started Command Scheduler.
2月 17 17:26:29 localhost.localdomain systemd[1]: Starting Command Scheduler...
2月 17 17:26:29 localhost.localdomain crond[1267]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 67% if used.)
2月 17 17:26:30 localhost.localdomain crond[1267]: (CRON) INFO (running with inotify support)
[root@test01 ~]# 

制订周期性计划任务

crontab -e

时间            COMMAND命令 

时间:
分 时 日 月 周

分钟:0----59 
时:  0----23
日期:1----31
月:  1----12
周:  0----6(0代表周日)

示例:

每天晚上11:30 30 23 * * *
每天零点 0 0 * * *

每周五下午4点 0 16 * * 5

  • 连续的时间 5 8-14 * * *

不连续的时间 5 8,10,12 * * *

每5分钟 */5 * * * *

COMMAND命令:
1 建议写命令的完整路径 /bin/mkdir /abc
2 只能写一条命令(shell脚本)

为什么要写命令的完整路径:


示例: 每分钟在/tmp目录创建文件

[root@localhost ~]# crontab -e
*/1 * * * * /usr/bin/touch /tmp/date +\%F-\%T.txt &> /dev/null
[root@localhost ~]#

注意:
写命令时%在周期性计划任务中是结束的意思,因此在使用%时,需要加\右斜杠转义
&> /dev/null 不给用户发送邮件

示例:每半个小时分别显示内存,CPU、磁盘使用状态信息

  1. 创建脚本文件
[root@test01 ~]# cat /root/1.sh 
#!/bin/bash

echo "内存容量:"
free -m
echo
echo "磁盘容量:"
df -hT
echo
echo "CPU负载:"
uptime

  1. 编辑计划任务
[root@test01 ~]# crontab -l
*/1 * * * *	/usr/bin/touch /tmp/`date +\%F-\%T`.log &> /dev/null
*/30 * * * * 	/usr/bin/bash /root/1.sh

查看周期性计划任务

[root@localhost ~]# crontab -l
*/1 * * * * /bin/touch /tmp/date +\%F-\%T.txt
[root@localhost ~]#

删除周期性计划任务

crontab -r >>> 会删除所有计划任务

在以上各个字段中,还可以使用以下特殊字符:

星号(*):代表所有可能的值,例如month字段如果是星号,则表示在满足其它字段的制约条件后每月都执行该命令操作。

逗号(,):可以用逗号隔开的值指定一个列表范围,例如,“1,2,5,7,8,9”

中杠(-):可以用整数之间的中杠表示一个整数范围,例如“2-6”表示“2,3,4,5,6”

正斜线(/):可以用正斜线指定时间的间隔频率,例如“0-23/2”表示每两小时执行一次。同时正斜线可以和星号一起使用,例如*/10,如果用在minute字段,表示每十分钟执行一次。

* 1 * * * 从1:0到1:59 每隔1分钟 执行
2 8-20/3 * * * 8:02,11:02,14:02,17:02,20:02 执行

Linux提示no crontab for root的解决办法:
这时会出现 “no crontab for root” 这是由于你还没有创建任何定时任务或者命令打错,即没有使用crontab -e命令去创建任何任务。但是到了这里,你的安装基本可以了。

安装crontab:
yum install crontabs

说明:
/sbin/service crond start //启动服务
/sbin/service crond stop //关闭服务
/sbin/service crond restart //重启服务
/sbin/service crond reload //重新载入配置

查看crontab服务状态:service crond status

手动启动crontab服务:service crond start

查看crontab服务是否已设置为开机启动,执行命令:ntsysv

加入开机自动启动:
chkconfig –level 35 crond on

查看进程:

ps aux |grep cron |grep -v 'grep'       //-v关闭grep进程

推荐阅读