Crontab介绍

Crontab是linux系统用来定期执行命令或程序的工具。

两种任务创建方式

  (系统级的)做系统级配置我们会直接配置 /etc/crontab  /etc/cron.d/

  (用户级的)一般还是建议大家使用 crontab -e ,这样系统也会帮着检查我们配置的脚本语法。

定期自动运行脚本

  也可以直接将需要定期执行的脚本放入以下目录中:

    /etc/cron.hourly/    # 被定义在 /etc/cron.d/0hourly 中每小时自动调用

    /etc/cron.daily/      # 被定义在 /etc/anacrontab 中每天自动调用

    /etc/cron.weekly/     # 被定义在 /etc/anacrontab 中每周自动调用

    /etc/cron.monthly/     # 被定义在 /etc/anacrontab 中每月自动调用

    

示例: logrotate 每天自动运行的脚本

[root@servera cron.d]# cd /etc/cron.daily/
[root@servera cron.daily]# ls
logrotate rhsmd
[root@servera cron.daily]# cat logrotate
#!/bin/sh /usr/sbin/logrotate /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != ]; then
/usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit $EXITVALUE

命令 Crontab

Usage:
crontab [options] file
crontab [options]
crontab -n [hostname]

Options:
-u <user> define user
-e edit user's crontab  # 以user名称保存路径 /var/spool/cron/
-l list user's crontab
-r delete user's crontab
-i prompt before deleting
-n <host> set host in cluster to run users' crontabs
-c get host in cluster to run users' crontabs
-s selinux context
-V print version and exit
-x <mask> enable debugging

Contab语法

# Example of job definition:
# .---------------- minute ( - )
# | .------------- hour ( - )
# | | .---------- day of month ( - )
# | | | .------- month ( - ) OR jan,feb,mar,apr ...
# | | | | .---- day of week ( - ) (Sunday= or ) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed

*    号表示任意时间都执行。

-    减号,表示分隔符,表示一个时间范围,区间段,如17-19点,每天的17,18,19点的00分执行任务。

,   逗号,表示分隔时段的意思。30 17,18,19 * * * /bin/sh /scripts/test.sh 表示每天17、18、19点的半点时刻执行/scripts/test.sh脚本。

/n   n代表数字,即“每个n单位时间”,例如:每10分钟执行一次任务,可以写成*/10 * * * * cmd,其中*/10的意思是每10分钟执行cmd命令。

书写定时任务的若干要领方法

要领1:为定时任务规则加必要的注释

要领2:定时任务命令或程序最好写到脚本里执行

要领3:执行shell脚本任务前加/bin/sh

要领4:定时任务执行的脚本要规范路径(/service/scripts)

要领5:定时任务命令或脚本结尾加>/dev/null 2>&1

具体例子:

● 0 */2 * * * /sbin/service httpd restart 意思是每两个小时重启一次apache

● 50 7 * * * /sbin/service sshd start 意思是每天7:50开启ssh服务

● 50 22 * * * /sbin/service sshd stop 意思是每天22:50关闭ssh服务

● 0 0 1,15 * * fsck /home 每月1号和15号检查/home 磁盘

查看Log

/var/log/cron

crond的更多相关文章

  1. linux定时任务crond export变量问题

    linux定时任务crond export变量问题 1)我写了一个重启resin的脚本,由于业务原因,需要定时在某一个时间重启下resin服务器,于是就在 crontab里配置了如下内容: 50 17 ...

  2. 如何让Linux定时任务crond以秒为单位执行(如每隔3秒)

    需要用到Shell脚本每隔3秒钟去监控一个软件进程的运行状态,发现crond似乎只支持到分,不知道秒,怎么办呢? 第一种方法: 当然首先想到的是写一个触发的脚本,在触发脚本中使用死循环来解决此问题,如 ...

  3. linux-------------计划任务crond:如何创建linux里面的计划任务

    1.centos下安装crond [root@localhost /]# yum -y install vixie-cron [root@localhost /]# yum -y install cr ...

  4. 配合crond服务实现自定义周期备份MySQL数据库(使用innobackupex进行备份)

    备份 新建一个脚本/root/backup.py,内容如下: #!/usr/bin/env python# -*- coding: utf-8 -*- ''' 脚本作者:昨夜星辰 脚本作用:配合cro ...

  5. CentOS下crond定时任务详细介绍

    目录 1.定时任务crond介绍... 2.crond定时任务限权... 3.Crontab用法... 4.Crontab命令的书写格式... 5.定时服务器时间同步... 6.写定时任务注意点.. ...

  6. crond不执行原因分析

    自己写了个脚本,让crond来周期性执行脚本进行备份,但是在crontab -e里面加入了执行脚本之后,发现没有执行,后来分析了一下,crond不执行的原因主要有以下几个方面: 1.crond服务没启 ...

  7. Linux服务器管理: 系统的定时任务crond

    cornd 是定时任务的守护进程  这个服务系统是默认启动的 [root@localhost/]#/etc/init.d/crond strat|restart|stop [root@localhos ...

  8. liunx系统计划任务管理(at/crond调度)

    一.at命令 at命令格式at HH:MM YYYY-MM-DD 其中 HH(小时):MM(分钟) YYYY(年)-MM(月份)-DD(日) 启动atd进程 /etc/init.d/atd start ...

  9. crond: unrecognized service 无crond解决办法

    运行计划任务时:service crond restart提示:crond: unrecognized service安装计划任务:yum -y install vixie-cron 另外附计划任务的 ...

  10. Linux crond定时任务

    第1章 Crond是什么? Crond是linux系统用来定期执行命令或指定程序任务的一种服务或软件.一般情况下,我们安装完Centos5/6linux操作系统之后,默认便会启动Crond任务调度服务 ...

随机推荐

  1. HTML5中两种方法实现客户端存储数据

    HTML5 提供了两种在客户端存储数据的新方法: localStorage - 没有时间限制的数据存储 sessionStorage - 针对一个 session 的数据存储 之前,这些都是由 coo ...

  2. .net core 2.0小白笔记(一):开发运行环境搭建

    小白一枚,有任何不妥之处敬请指教 这里不讨论什么设计模式,什么架构,什么什么,就是入门,简单的入门,虽然能跨平台,但是这里还是在win的环境下进行,不扯的那么远 其实官网文档写的挺不错的了,就是偶尔有 ...

  3. Python Windows文件操作

    获得目录和文件名 os.getenv()获取环境变量 os.putenv()设置环境变量 os.getcwd() 获得当前目录 os.chdir(‘要设置的当前目录’) os.listdir() 返回 ...

  4. Https单向认证和双向认证介绍

    一.Http HyperText Transfer Protocol,超文本传输协议,是互联网上使用最广泛的一种协议,所有WWW文件必须遵循的标准.HTTP协议传输的数据都是未加密的,也就是明文的,因 ...

  5. unity4.6 Beta版 UI控件之Button

    近期需求,须要用到4.6版本号uGui了,所以抽时间来学习学习,就UI控件在Unity工具里创建预设这块来说相比較于NGUI,我认为是没有什么太大的差别的. 比方:Canvas--Camera . T ...

  6. Exploiting CVE-2015-2509 /MS15-100 : Windows Media Center could allow remote code execution

    Exploiting CVE-2015-2509 /MS15-100 : Windows Media Center could allow remote code execution Trend Mi ...

  7. 关于quartusII 错误 Error: Current license file does not support the EP*** device 错误原因总结

    关于quartusII 错误 Error: Current license file does not support the EP*** device 错误原因总结 第一,有的人用了破解文件lice ...

  8. [转]mysqlx 同时使用 AND OR

  9. shiro 实现自己定义权限规则校验

    <span style="font-family: Arial, Helvetica, sans-serif;">在系统中使用shiro进行权限管理,当用户訪问没有权限 ...

  10. Lumen开发:lumen源码解读之初始化(4)——服务提供(ServiceProviders)与路由(Routes)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前面讲了singleton和Middleware,现在来继续讲ServiceProviders和Routes,还是看起始文件bootstrap/a ...