定期执行任务

   在Linux中经常需要我们去执行配置文件的读取、日志文件的保存等定期执行的任务。可以通过一些配置让系统自动执行这些任务。

   定期任务执行:

1、在未来的某个时间仅执行一次

   # at

   # batch

at  and batch read commands from standard input or a specified file which are to be executed at a later time .

   其时间表示法:

   HH:MM [AM|PM]

   MM/DD/YY

 

   at now+#

         miniutes

         hours

         days

         weeks

  例:# at now+10min 

  可以输入 Ctrl+d 来提交at任务

   at -l = atq  查看列表

   at -r job_num = atrm job_num 清除任务

   这些任务的执行输出一般都通过mail方式发给当前用户:

     # mail 

        Mail version 8.1 6/6/93.  Type ? for help.

"/var/spool/mail/root": 2 messages 1 new 2 unread

 U  1 [email protected]  Sun Feb 26 17:47  46/1685  "Logwatch for localhost.localdomain (Linux)"

>N  2 [email protected]  Sun Feb 26 18:47  16/696   "Output from your job        2"

  输入数字可以查看mail

  q键退出

 

   at -f file TIME  从某一文件中读取

 

   batch不需要加时间,用法和at一样  

 

 

2、在未来周期性去执行一个任务:

   crontab

   anacron

 

 

crond    属于服务,在后台一直运行。其环境变量与用户不同。

chkconfig --list    查看服务列表

which crond         查看crond根路径

rpm -qf /usr/sbin/crond  查看crond的安装包

service crond status     查看crond的状态

 

定义环境变量:

PATH=/sbin:/bin:/usr/sbin:/usr/bin

 

 /var/spool/cron/USERNAME   用户定义的任务计划

 /etc/crontab              系统定义的任务计划

两类:

1、用户cron    /var/spool/cron/USERNAME  不需要指定用户  配置文件有六个字段

 

2、系统cron    /etc/crontab           需要指定用户   配置文件有七个字段

#vim /etc/crontab

   SHELL=/bin/bash     

   PATH=/sbin:/bin:/usr/sbin:/usr/bin

   MAILTO=root  

   HOME=/

 

   # run-parts

   01 * * * * root run-parts /etc/cron.hourly

   02 4 * * * root run-parts /etc/cron.daily

   22 4 * * 0 root run-parts /etc/cron.weekly

   42 4 1 * * root run-parts /etc/cron.monthly

 

时间:

min:01-59

hour:00-23

day: 01-31

month: 01-12

day of week: 00-07

 

4 7 * * *   每天的七点四分    每天任务

10 * * * *  每十分钟          每小时任务

10 2 3 * *  每月三号两点十分  每月任务

30 4 * * 6  每周六四点三十分  每周任务

 

非连续时间点:,

15,45 * * * *              每半小时任务

连续时间点:-

15 8-18 * * *              每小时任务

 

*/#  

*/3 * * * *   每三分钟执行一次

00-59/3 * * * * 每三分钟执行一次

 

 

crontab  

# crontab -e 编辑crontab文件

*/3 * * * * /bin/echo "hello"

# crontab -l 列出crontab文件

# crontab -r 移除crontab文件

# crontab -u USERMANE 编辑特定用户的crontab文件

 

*/2 * * * * /sbin/enableppp &> /dev/null

40 21 * * * /sbin/shutdown -h now &> /dev/null

10 3 * * * /bin/tar -jcf /baskup/etc-`date +%F`.tar.bz2 /etc

 

anacron: 实现检查过期和未完成的crontab的任务

# vim /etc/anacrontab

   # /etc/anacrontab: configuration file for anacron

 

   # See anacron(8) and anacrontab(5) for details.

 

   SHELL=/bin/sh

   PATH=/sbin:/bin:/usr/sbin:/usr/bin

   MAILTO=root

 

   1       65      cron.daily              run-parts /etc/cron.daily

   7       70      cron.weekly             run-parts /etc/cron.weekly

   30      75      cron.monthly            run-parts /etc/cron.monthly

 

注:date   系统时间

clock  硬件时间

 

hwclock -w 将date写入clock

        -s 将clock写入date

  # crontab -e

   */3 * * * * /sbin/hwclock -s &> /dev/null

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章