在开始处理脚本时,总会遇到需要在给定时间(通常是人不在的时候)运行脚本,Linux系统提供了3种方式在预定时间运行脚本:
at命令
batch命令
cron表格
at命令--执行一次性计划任务
at [-f filename] time
默认情况下,at命令将STDIN输入提交给队列,可以用-f参数指定用于读取命令的脚本文件
time参数指定linux系统运行的时间:
标准小时制和分钟格式:10:15
AM/PM格式:10:15PM
具体指定时间:now noon midnight teatime
如果指定一个运行作业过去的时间,它将会在第二天运行
指定时间增量:
Now+25 minutes
10:15PM tomorrow
10:15 + 7 days
安装:at并启动服务 [root@localhost ~]# yum install at [root@localhost ~]# /etc/init.d/atd start 正在启动 atd: [确定] [root@localhost ~]# /etc/init.d/atd status atd (pid 1071) 正在运行...
默认情况下,所有at作业都提交到队列a,即最高的优先级队列! 示例: #!/bin/bash time=`date +%T` echo "Thi is script ran an $time" echo "Thi is the end of the script" >&2
atq命令列出排队的作业: [root@localhost ~]# atq 42014-11-11 14:30 a root 32014-11-11 14:15 a root
atrm命令移除排队的作业: [root@localhost ~]# atq 42014-11-11 14:30 a root 32014-11-11 14:15 a root [root@localhost ~]# atrm 4 [root@localhost ~]# atq 32014-11-11 14:15 a root 说明:Ctrl+d表示编写完成
batch命令 batch命令和at命令有些不同,batch命令作用不是安排脚本在预定的时间运行,而是安排在系统使用率低时运行 batch命令检查linux系统当前的平均负载水平,如果平均负载低于0.8!它将运行在队列中等待的作业 命令格式: batch [-f filename] [time]
cron定期脚本命令
cron使用特殊格式指定作业运行的时间
cron命令格式:
min hour dayofmonth month dayofweek command
cron表格可以将条目指定为具体值,一定的范围值(1-5)或者(星号)
示例: 1.每天10:15运行命令 15 10 * * * command 2.每周一下午4:15运行命令 15 16 * * 1 command 3.每月第一天中午12点运行命令 00 12 1 * * command
注意:因为不能将dayofmonth设置为涵盖所有月份,所有我们要用date检查下明天是否为1号:
00 12 1 * * * if [ `date +%d -d tomorrow` = 01 ] ; then ; command
这将在每天12点查看本日是否为该月的最后一天,如果是,它将运行命令!
命令列表必须指定要运行命令或shell脚本的完整路径名!、
crontab -e 进入任务编辑
crontab -l 查看任务计划
转载于:https://blog.51cto.com/liangey/1575314