智一面Gtalent的运维如何使用crontab命令

Crontab是linux的定时任务的守护进程,精确到分

Centos的安装命令

yum -y install vixie-cron crontabs

ubuntu的安装命令

apt-get install cron

Contos

# 查看cond 状态
service crond status

# 启动cron
service crond start

# 关闭cron
service crond stop

# 重启cron
service crond restart

Ubuntu

# 查看cond 状态
service cron status

# 启动cron
service cron start

# 关闭cron
service cron stop

# 重启cron
service cron restart

命令格式

crontab [-u user] file

usage:  crontab [-u user] file

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

                (default operation is replace, per 1003.2)

        -e      (edit user's crontab)

        -l      (list user's crontab)

        -r      (delete user's crontab)

        -i      (prompt before deleting user's crontab)

        -s      (selinux context)

minute   hour   day   month   week   command
# 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  表示星期(0 或 7 表示星期天)
|  |  |  |  |  .------------------- username  以哪个用户来执行
|  |  |  |  |  |            .------ command  要执行的命令,可以是系统命令,也可以是自己编写的脚本文件
|  |  |  |  |  |            |
*  *  *  *  * user-name  command to be executed123456789101112

 

格式示例:

 

  */1 * * * * service httpd restart
  每一分钟 重启httpd服务


  0 */1 * * * service httpd restart
  每一小时 重启httpd服务


  30 21 * * * service httpd restart
  每天 21:30 分 重启httpd服务


  26 4 1,5,23,28 * * service httpd restart
  每月的1号,5号 23 号 28 号 的4点26分,重启httpd服务


  26 4 1-21 * * service httpd restart
  每月的1号到21号 的4点26分,重启httpd服务


  */2 * * * * service httpd restart
  每隔两分钟 执行,偶数分钟 重启httpd服务


  1-59/2 * * * * service httpd restart
  每隔两分钟 执行,奇数 重启httpd服务


  0 23-7/1 * * * service httpd restart
  每天的晚上11点到早上7点 每隔一个小时 重启httpd服务


  0,30 18-23 * * * service httpd restart
  每天18点到23点 每隔30分钟 重启httpd服务


  0-59/30 18-23 * * * service httpd restart
  每天18点到23点 每隔30分钟 重启httpd服务


  59 1 1-7 4 * test 'date +\%w' -eq 0 && /root/a.sh
  四月的第一个星期日 01:59 分运行脚本 /root/a.sh ,命令中的 test是判断,%w是数字的星期几

例子命令:

crontab -e -u root

然后 输入我们的

* * * * * 你的命令

内容来源:http://www.gtalent.cn/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值