cron计划任务之用户级

  • 1.安装crond
centos7 执行命令:

# yum install -y crontabs
/bin/systemctl restart crond.service #重启服务
/bin/systemctl status crond.service #查看crontab服务状态
  • 2.crond进程每分钟会处理一次计划任务 ,存储位置在 /var/spool/cron/
  • 3.管理方式
 crontab -l 列出当前用户的计划任务
crontab -r 删除当前用户所有的计划任务
crontab -e 编辑当前用户的计划任务

管理员可以使用 -u username,去管理其他用户的计划任务

示例

[root@Server-n93yom ~]# crontab -e //编辑一个计划任务* * * * * date >> /root/tmp/huhu.job   后面的命令可以替换为一个脚本/*.sh
[root@Server-n93yom ~]# crontab -l
* * * * * date >> /root/tmp/huhu.job
[root@Server-n93yom tmp]# tailf huhu.job
Wed Sep 18 23:30:01 CST 2019
[root@Server-n93yom tmp]# crontab -r
[root@Server-n93yom tmp]# crontab -l
no crontab for root
  • 4./etc/cron.deny 中定义的是禁止某用户去执行计划任务

  • 5.语法格式

第一列代表分钟(0-59)

第二列代表小时(0-23)

第三列代表天(1-31)

第四列代表月(1-12)

第五列代表周(0-6) 0表示周日

“*”代表所有的取值范围内的数字。如果直接用“*”则相当于“*/1”

“/”代表每的意思

“*/5″表示每5个单位

“-”代表从某个数字到某个数字

“,”分散的数字

# 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

# |  |  |  |  |

# *  *  *  *  *  command to be executed

cron计划任务之系统级

  • 系统计划任务的作用

    - 临时文件的清理 /tmp /var/tmp

    - 系统信息的采集

    - 日志的轮转 (切割)logrotate
  • cron系统任务文件:/etc/crontab //该文件中默认没有定义任何计划任务 ,建议用用户级的计划任务
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root # 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 \n* * * * * root /etc/cron.hourly/logstash_heartbeat
  • 系统任务文件:ls /etc/cron.d/* //crond仅仅会执行每小时定义的脚本 ; /etc/cron.hourly 该文件夹下有个0anacron进程,每小时的01分钟会唤醒anacrond进程

    - anacrond详解及命令
[root@Server-n93yom ~]# ls /etc/cron.d/*
/etc/cron.d/0hourly
[root@Server-n93yom ~]# cat /etc/cron.d/0hourly
# Run the hourly jobs
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
01 * * * * root run-parts /etc/cron.hourly
[root@Server-n93yom ~]# ls /etc/cron.hourly
0anacron logrotate logstash_heartbeat
[root@Server-n93yom ~]# ls /var/spool/anacron/
cron.daily cron.monthly cron.weekly //anacron下的计划任务
[root@Server-n93yom ~]# cat /etc/anacrontab //查询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
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command //每小时的01分anacron进程会检查每天,每周或者每月的定时任务,若之前因为停机异常等导致的错过的任务,会后续补上
1 5 cron.daily nice run-parts /etc/cron.daily //每天的任务若错过,则会在整点延后五分钟补上上次遗漏的任务
7 25 cron.weekly nice run-parts /etc/cron.weekly //每周的任务若错过,则会在整点延后25分钟补上上次遗漏的任务
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly //每月的任务若错过,则会在整点延后25分钟补上上次遗漏的任务
  • cron日志
[root@Server-n93yom ~]# tailf /var/log/cron
Sep 19 23:01:01 Server-n93yom run-parts(/etc/cron.hourly)[1355]: finished 0anacron
Sep 19 23:01:01 Server-n93yom run-parts(/etc/cron.hourly)[1346]: starting logrotate
Sep 19 23:01:02 Server-n93yom run-parts(/etc/cron.hourly)[1362]: finished logrotate
Sep 20 00:01:01 Server-n93yom CROND[5576]: (root) CMD (run-parts /etc/cron.hourly)
Sep 20 00:01:01 Server-n93yom run-parts(/etc/cron.hourly)[5576]: starting 0anacron
Sep 20 00:01:01 Server-n93yom anacron[5585]: Anacron started on 2019-09-20
Sep 20 00:01:01 Server-n93yom anacron[5585]: Normal exit (0 jobs run)
Sep 20 00:01:01 Server-n93yom run-parts(/etc/cron.hourly)[5587]: finished 0anacron
Sep 20 00:01:01 Server-n93yom run-parts(/etc/cron.hourly)[5576]: starting logrotate
Sep 20 00:01:01 Server-n93yom run-parts(/etc/cron.hourly)[5594]: finished logrotate

最新文章

  1. AFNetworking 3.0 源码解读(二)之 AFSecurityPolicy
  2. 【Codeforces163E】e-Government AC自动机fail树 + DFS序 + 树状数组
  3. 如何让jboss eap 6.2+ 的多个war应用共享 jar 包?
  4. MySQL数据库的基本数据类型
  5. [BZOJ1477]青蛙的约会
  6. 《Java程序设计》第6周学习总结
  7. 如何通过session控制单点登录
  8. Windows Phone 8 蓝牙编程
  9. Upload/download/UrlConnection/URL
  10. 使用Windbg来检查内存
  11. Linux网络配置相关
  12. display的none与block(判断登录界面的账号密码是否为空)
  13. Linux进程实践(2) --僵尸进程与文件共享
  14. Linux命令之常用篇
  15. tool 使用font-spider解决字体压缩问题
  16. 【持续更新】JAVA面向对象多线程编程的一些tips
  17. 【Linux】使用Google Authenticator 实现ssh登录双因素认证
  18. Java编程的逻辑 (88) - 正则表达式 (上)
  19. [leetcode]689. Maximum Sum of 3 Non-Overlapping Subarrays三个非重叠子数组的最大和
  20. python 中的__del__

热门文章

  1. 【MySQL作业】多字段分组和 having 子句——美和易思分组查询应用习题
  2. 为什么操作dom会消耗性能
  3. hisql ORM 框架研究(国内第一个支持HANA的ORM框架)
  4. Centos7 用户权限相关
  5. Centos7 文件修改详情
  6. springBoot--01--快速入门
  7. 【代码优化】Bean映射之MapStruct
  8. 《剑指offer》面试题68 - I. 二叉搜索树的最近公共祖先
  9. [Keil 学习] printf, scanf函数的用法
  10. ROS之face recongination(cbo_peopel_detection)