linux下面,在某一个时间去执行程序或者任务

1、一次性的计划任务

2、周期性的计划任务


一、一次性的周期性任务

1)、计划任务(at服务)

1、首先查找at服务是否安装
  # rpm -q at
  at-3.1.10-43.el6_2.1.x86_64


2、查看软件列表
  /etc/at.deny 用户拒绝列表  at.allow 默认不存在,用户允许列表,需要自己创建
  /etc/pam.d/atd 认证文件
  /etc/rc.d/init.d/atd 服务的启动脚本
  /usr/bin/at 制定计划任务的命令
  /usr/bin/atq 查看计划任务列表 at -l
  /usr/bin/atrm 删除计划任务 at -d
  /usr/bin/batch 优先级
  /usr/sbin/atd 二进制命令
  /var/spool/at 计划任务的存放目录    


3、服务是否启动,必须处于运行级别。
   # /etc/rc.d/init.d/atd status   
    atd (pid  2179) is running...  服务启动的状态

4、service atd restart 启新启动服务


5、临时命令执行

at now 当前执行

#at now
  at> echo 88 >> /tmp/test  把88追加重定向到/tmp/test文件中
  at> <EOT>                 ctrl+c 退出
  job 5 at 2016-09-19 18:59  执行的时间

at now +1min    当前时间1分钟之后执行
  at 3:30pm       下午3:30执行
  at 4pm +3days   三天之后的下午4点执行
  at -t 09291430   9月29号14点30分执行

-d  删除计划任务

-l   列出计划任务

-f   指定文件

-c  查看计划任务的内容


 dome1:比较复杂的
 vim useradd.sh
 #!/bin/bash
   for i in 1 2 3
   do
     useradd stu$i
   done
   echo user is ok! > /tmp/log1
  # chmod +x useradd.sh   给予的文件的执行权限
  # at now +3min -f /tmp/useradd.sh   3分钟执行useradd.sh文件


 /etc/at.deny  黑名单 该文件里的用户不能使用at服务   
  /etc/at.allow 白名单  只允许在该文件里的用户使用at服务   
  如果at.deny和at.allow 文件冲突,at.allow优先


二、周期性的计划任务

1、查看软件按是否安装
  # rpm -q cronie
   cronie-1.4.4-12.el6.x86_64
   # rpm -q crontabs
   crontabs-1.10-33.el6.noarch
  周期计划任务需要两个软件包支持

 

2、查看软件的列表
 # rpm -ql cronie  
  /etc/cron.d 该服务的主目录
  /etc/cron.d/0hourly 系统每小时的第1分钟需要执行的计划任务
  /etc/cron.deny 用户拒绝列表 cron.allow 用户允许列表(默认不存在)
  /etc/pam.d/crond 认证文件
  /etc/rc.d/init.d/crond 启动脚本
  /usr/bin/crontab 定制计划任务命令
  /usr/sbin/crond 二进制命令
  /var/spool/cron 存放用户级别的周期下计划任务目录

# rpm -ql crontabs
  /etc/cron.daily 系统每天需要执行的计划任务
  /etc/cron.hourly 系统每小时
  /etc/cron.monthly 系统每月
  /etc/cron.weekly 系统每周
  /etc/crontab  系统计划任务的配置文件
  /usr/bin/run-parts 工具,后面跟目录
  /usr/share/man/man4/crontabs.4.gz man文档


 3、确认服务是否安装启动
  # service crond status
    crond (pid  2697) is running..


4、系统级别计划任务
   /etc/crontab   

SHELL=/bin/bash  指定默认shell
PATH=/sbin:/bin:/usr/sbin:/usr/bin  命令环境变量
MAILTO=root 发送邮件给root;MAILTO=""代表不发送邮件
HOME=/ 命令和文件的根目录

# 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

  ,号分割  * 1,4,5 * * * 每天的1点 4点 5点
  - 号分割  * * 1-5 * *  每月的1号到5号
  / 号分割  */5 * * * * 每隔5分钟

demo1:
  vim /etc/crontab
  */1 * * * * root echo $(date)>/tmp/log1  每分钟执行的一次
  */1 * * * * stu1 id stu1 >>/tmp/log1
       每分钟执行的一次


demo2:
  方法一:
  #vim /etc/cron.d/test
   SHELL=/bin/bash
   PATH=/sbin:/bin:/usr/sbin:/usr/bin
   MAILTO=root
   HOME=/
   30 04 * * * root run-parts /tmp/test
   
   run-parts工具:后面跟的是一个目录,执行该目录下面的所有可执行的脚本文件

方法二:
  cp /tmp/test/*.sh /etc/cron.daily

  chmod +x /tmp/test/*.sh

  anacron :是cron服务的一个扩充服务,作用:当系统意外宕机而错过了任务的执行时间,它会等到系统开机后延迟一段时间再次补漏。

  # cat /etc/anacrontab
  #period in days   delay in minutes   job-identifier   command
  1     5    cron.daily        nice run-parts /etc/    cron.daily

  7     25    cron.weekly        nice run-parts /etc/cron.weekly
  @monthly  45    cron.monthly        nice run-parts /etc/cron.monthly
   

三、用户级别计划任务
  crontab 命令
  root用户:可以给自己定制周期性的任务,同时还可以给普通用户制定计划任务
  -e 编辑  默认调用vi编辑器  export EDITOR=vim|gedit  自定义编辑器
  -l 查看
  -r 删除
  -u 指定用户
  -eu username
  -lu username
  -ru username
 
 demo1:
 #vim   /root/time.sh
 #!/bin/bash
    ntpdata  -u 192.168.1.2
    echo  "system  data  is  ok " >>/tmp/test.log
                
  #chmod   +x  time.sh
        
    说明:周期后面直接跟文件的绝对路径,文件要有可执行权限              
                        
demo2 :每个一分钟stu2 给stu1 用户发一封邮件                       
     stu2 $  crontab  -l
      * /1 * * * *  mail  -s  "test"  stu1 </etc/hosts
 
    # mail  -s  "test"  stul  < /etc/hosts
     # echo  hahahaha  |mail  stu1

 
 cron 服务的用户访问控制: /etc/cron.deny
                       /etc/cron.allow