Crontab 命令

crontab概念:

crontab命令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令。

工具地址:

https://tool.lu/crontab/

服务相关命令:

# 安装
yum -y install vixie-cron crontabs
#查看状态
service crond status
#启动
service crond start
#关闭
service crond stop
#重启
service crond restart

crontab 命令:

    crontab [-u user] file
    crontab [-u user] [ -e | -l | -r ]
  • 命令参数:
-u user:用来设定某个用户的crontab服务,例如,“-u ixdba”表示设定ixdba用户的crontab服务,此参数一般有root用户来运行。

file:file是命令文件的名字,表示将file做为crontab的任务列表文件并载入crontab。如果在命令行中没有指定这个文件,crontab命令将接受标准输入(键盘)上键入的命令,并将它们载入crontab。

-e:编辑某个用户的crontab文件内容。如果不指定用户,则表示编辑当前用户的crontab文件。

-l:显示某个用户的crontab文件内容,如果不指定用户,则表示显示当前用户的crontab文件内容。

-r:从/var/spool/cron目录中删除某个用户的crontab文件,如果不指定用户,则默认删除当前用户的crontab文件。

-i:在删除用户的crontab文件时给确认提示

crontab格式:

每一行都代表一项任务,每行的每个字段代表一项设置,它的格式共分为六个字段,前五段是时间设定段,第六段是要执行的命令段,格式如下:

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 executed

案例:

一个普通标题一个普通标题
*/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是数字的星期几
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值