crontab使用教程

一、什么是crontab

crontab命令常见于Unix和类Unix的操作系统之中,用于设置周期性被执行的指令。该命令从标准输入设备读取指令,并将其存放于“crontab”文件中(是“cron table”的简写),以供之后读取和执行。crontab储存的指令被守护进程激活, crond常常在后台运行,每一分钟检查是否有预定的任务需要执行,此任务被称为cron jobs

简单讲:crontab 用于定时执行任务

二、怎么使用

1、 检查当前系统是否已安装crontab

Mac OS默认已安装crontab命令,如下打印说明已安装

~/workspace/wicc/WaykiChain_mainnet$crontab -h
crontab: illegal option -- h
crontab: usage error: unrecognized option
usage: crontab [-u user] file
       crontab [-u user] { -e | -l | -r }
~/workspace/wicc/WaykiChain_mainnet$

2、crontab命令的功能

通过crontab 命令,我们可以在固定的间隔时间执行指定的系统指令或 shell script脚本。时间间隔的单位可以是分钟、小时、日、月、周及以上的任意组合。这个命令非常设合周期性的日志分析或数据备份等工作。

3、参数意义

-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文件时给确认提示。

4、使用实例

(1) 设置每小时执行一次checkblock.sh脚本,并将log输出到指定文件

准备需要执行的checkblock.sh脚本,脚本内容可忽略

~/workspace/wicc/WaykiChain_mainnet$pwd
/Users/wujinquan/workspace/wicc/WaykiChain_mainnet
~/workspace/wicc/WaykiChain_mainnet$ls
bin		checkblock.sh	conf		data
~/workspace/wicc/WaykiChain_mainnet$
~/workspace/wicc/WaykiChain_mainnet$cat checkblock.sh
#!/bin/bash

echo "start check to blockcount:"

date

blockfile="wicc.blocks"
touch $blockfile
past_blockcount=`tail -n 1 $blockfile`
echo  "past blockcount:$past_blockcount"

current_blockcount=`docker exec -it waykicoind-main /opt/wicc/coind  getblockcount`
echo "current blockcount:$current_blockcount"

if [ $current_blockcount = $past_blockcount ]
then
  echo "current blockcount equal to past blockcount, so restart the waykicoind."
  docker restart waykicoind-main
else
  echo "current blockcount does not equal to past blockcount, so go ahead."
  past_blockcount=$current_blockcount
fi

echo $current_blockcount>$blockfile
echo "Now the past blockcount is $past_blockcount "

echo "end!"

~/workspace/wicc/WaykiChain_mainnet$
(2) 此处需要用到 crontab -e 命令创建定时任务

NOTE : mac OS 默认需要在root模式下创建任务,不然普通用户邮箱会收到如下权限限制提示

/bin/sh: /Users/wujinquan/workspace/wicc/WaykiChain_mainnet/checkblock.sh: Permission denied
/Users/wujinquan$crontab -l  //查询当前的任务,当前没有任务
crontab: no crontab for root
/Users/wujinquan$crontab -e  //创建目标任务
crontab: no crontab for root - using an empty one
crontab: installing new crontab

执行crontab -e (类似vi)之后,输入以下命令

0 */1 * * *   /Users/wujinquan/workspace/wicc/WaykiChain_mainnet/checkblock.sh >> /Users/wujinquan/workspace/wicc/WaykiChain_mainnet/checkblock.log

执行crontab -l查询当前的任务

/Users/wujinquan$crontab -l
0 */1 * * *   /Users/wujinquan/workspace/wicc/WaykiChain_mainnet/checkblock.sh >> /Users/wujinquan/workspace/wicc/WaykiChain_mainnet/checkblock.log
/Users/wujinquan$
(4) 任务创建的规则

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

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

Example

格式 说明
*/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服务
(3) 查看任务时候执行

目录下多了checkblock.sh,说明定时任务执行成功了

~/workspace/wicc/WaykiChain_mainnet$ls
bin		checkblock.log	checkblock.sh	conf		data
~/workspace/wicc/WaykiChain_mainnet$
发布了32 篇原创文章 · 获赞 15 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/u010159567/article/details/86258441