动态线条
动态线条end

cron:周期性计划任务、循环性计划任务,主要用于备份数据。 

   crontab       

       usage:crontab [-u user] [ -e | -l | -r ]     

       -u:<user>  define user      #指定用户     

       -e:edit user's crontab      #编辑计划任务     

       -l:list user's crontab      #查看计划任务     

      -r:delete user's crontab      #清空计划任务

1、crontab -e      编辑周期性计划任务的内容
时间
min     hour   day  month  week    command
分钟     小时      日      月         周      动作(任务命令)    
0-59     0-23    1-31   1-12      0-7    命令或脚本(写绝对路径)
30          2          1        *           *       rm -rf /tmp/*   //每月1号的2:30分删除/tmp目录下的所有文件
5            *           *        6          3       rm -rf /tmp/*   //每年6月的每周三每小时的5分钟删除/tmp目录下的所有文件
6            6          *        6           6       rm -rf /tmp/*    //每年6月的周六的6点6分钟删除/tmp目录下的所有文件

2、时间数值的特殊表示方法

*  表示该范围内的任意时间
,  表示间隔的多个不连续时间点
-   表示一个连续的时间范围
/   指定间隔的时间频率  

3、时间的写法

The time and date fields are:

      field          allowed values
      -----          --------------
      minute          0-59
      hour              0-23
   day of month    1-31
      month          1-12  (or  names,
      see below)
      day  of  week    0-7  (0 or 7 is
      Sun, or use names)

4、案例
案例一:每个星期三的下午10:00备份/etc/目录到/tmp目录叫2017-04-12_etc.tar.gz备份文件包含当天日期
crontab -e
* 22 * * 3 /usr/bin/tar cvzf /tmp/`/usr/bin/date +%F`_etc.tar.gz /etc 
注:`命令`中的``为命令替换功能,系统会先执行``(反撇,反引号)中的命令.也可以将``写成$()
案例二:只保留最近1个月的日志文件
crontab -e
1 * * * *  find /var/log -mtime +30 |xarge -i rm -rf {}
案例三:每月1号的凌晨2点半清空临时盘
crontab -e
30  2  1  *  *  /usr/bin/rm -rf /tmp/* 
案例四:每周每天的晚上8:10关闭电脑,除了星期六,星期五,星期日的17:10关闭电脑
crontab -e
10 20 * * 1-4  /usr/sbin/poweroff
10 17 * * 5,7  /usr/sbin/poweroff
案例五:jim用户每天在家目录创建一个以日期命名的目录20170412
crontab -u jim -e
1  0  *  *  *  /bin/mkdir  /home/jim/`date +%Y%m%d`

 

 


查询周期性计划任务的内容
      crontab -l      #查看当前用户
      crontab -u jim -      #查看指定用户
删除某用户的所有周期性计划任务(删除某个计划任务crontab -e 进入文本删除)
      crontab -r      #当前用户
      crontab -u jim -r      #指定用户


系统定义的计划任务(默认)
      更新whatis数据库
      更新updatedb数据库
      日志定期轮滚(日志切割)
      清理/tmp /var/tmp
      收集系统的状态信息
 
vim /etc/crontab  全局配置文件   

SHELL=/bin/bash

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

MAILTO=root

HOME=/

 

# For details see man 4 crontabs


# 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  

/etc/cron.*/  系统默认的设置文件


以下目录内可以存在脚本  该脚本就会按照时间去执行
      cd /etc
      cron.hourly/      #每小时
      cron.weekly/      #每周
      cron.daily/      #每天
      cron.monthly/      #每月

/var/spool/cron/用户名  用户定义的设置文件


5、删除超过一个月不使用的文件
      cat /etc/cron.daily/tmpwatch
6、crontab服务管理:
      查软件是否已安装:rpm  -q  crontabs
      查软件的文件列表:rpm  -ql  crontabs
      查软件的配置文件列表:rpm -qc crontabs
      服务状态:systemctl   status   crond       注:d是daemon守护进程
      重启at服务: systemctl   restart   crond   或  service   crond  restart
      关闭at服务: systemctl   stop   crond   或  service   crond  stop
      开机不启动at服务: systemctl   disable  crond 或 chkconfig   crond  off
      开机启动at服务:  systemctl   enable   crond  或 chkconfig   crond  on
7、cron权限管理
/etc/cron.allow      白名单:只允许在此名单内的用户使用crontab(此文件本身不存在)
/etc/cron.deny      黑名单:在此名单内的用户不可以使用crontab
      注:两个文件都不存在,只有root用户可以使用crontab
             两个文件都存在,allow优先执行

随想 

date -s “ 2017-12-30 00:00:00 ”

指定任意时间

date 

查看时间

ntpdate pool.ntp.org

 校对时间

 

posted on 2019-08-09 11:37  玉树临风的猪  阅读(580)  评论(0编辑  收藏  举报