crontab使用方法和示例

crond是linux中的一个定时任务常驻程序,它会在每分钟检查一次作业列表,从而达到在指定时间自动运行指定的作业,这个程序对于系统运维来讲必不可少。

通常我们使用crontab程序来设定和管理作业的,具体启动执行则是crond deamon。下面讲述如何使用crontab来设定使得系统在您指定的时间运行特定的任务。

步骤说明:

1. 编写你想要执行的作业的shell 脚本。

2. 使用chmod 修改shell脚本的mod为可执行。

3. 将脚本添加到crontab配置文件中,并指定其启动运行的时间。

1. 编写 shell 脚本

    编写shell脚本的方法不是本文的主要内容,请参阅:shell脚本编写方法 ;shell 常用的条件判断与条件测试 shell 中数据传递方法

    现假定你已经正确地编写了一个shell script, 名叫 myShellScript.sh 在您的个人目录下。

2. 使用chmod修改shell脚本mod为可执行

    使用如下指令来修改mod :

chmod +x myShellScript.sh 

    特殊情况下,如果你的shell脚本中使用了运行中需要root相关权限的命令,则需要修改对应的文件mod,使得其运行期自动提升为root权限。    

chmod +s xxxxx ;

3. 将脚本添加到crontab配置文件中,并指定其启动运行的时间。

crontab命令的惯常用法是:

crontab -l #用于查看当前用户的定时作业。

crontab -e #会打开编辑器,加载作业设置文件,用于编辑、添加、删除定时作业。

通常我们会先用crontab -l 来查看当前的配置,然后使用crontab -e 来启动编辑器, 将我们所需的任务添加进去。

配置crontab定时作业就必定包含两类信息:一个时间设定,一个目标程序。

其文件的格式为:

# 格式说明
# ——分钟 (0 - 59)
# | ——小时 (0 - 23)
# | | ——日 (1 - 31)
# | | | ——月 (1 - 12)
# | | | | ——星期 (0 - 7)(星期日=0或7)
# | | | | |
# * * * * * 被执行的命令

    上述可以看出,五个* ,一个jobName, 分别用于定义时间频度 和 作业名称。

时间指定详细设计
crontab配置文件支持在【分时日月周】任何一个时间项里填写多个数值:

1. 逗号 (',') 分开的值,例如:“1,3,4,7,8”
2. 连词符 ('-') 制定值的范围,例如:“1-6”,意思等同于“1,2,3,4,5,6”
3. 星号 ('*') 代表任何可能的值。例如,在“小时域” 里的星号等于是“每一个小时”,等等
4. 某些cron程序的扩展版本也支持斜线 ('/') 操作符,用于表示跳过某些给定的数。例如,“*/3”在小时域中等于“0,3,6,9,12,15,18,21”等被3整除的数;

例如:

*/5   1,2,3-9/3    1    1   *   touch `date "+%Y%m%d"`

以上设置表明:用户想要在每年的1月1日1点,2点 ,还有3到9中能被3整除的点,每隔5分钟运行一次。

注意要点:

第三项 与  第五项 都定义的情况下, 两者是或的关系, 如:

59 1 1-7 4 0 /root/shift_my_times.sh

这个程序会在4月1日至7日以及4月余下的每一个星期日执行。

最后说明一下:

高级用法:

cron有八个特殊符号,使用特殊符号不但节省时间,也更直观一些,以下是符号说明: 

特殊符号           意义
@reboot          重启时运行一次
@yearly          每年运行一次,相当于: "0 0 1 1 *".
@annually       跟@yearly意思一样
@monthly       每月运行一次, 相当于: "0 0 1 * *".
@weekly         每周运行一次,相当于 "0 0 * * 0".
@daily            每天运行一次, 相当于"0 0 * * *".
@midnight      跟@daily的意思一样)
@hourly          每小时运行一次, 相当于"0 * * * *".

Demo示例:每隔一小时运行一下/root/mkdir.sh:

@hourly /root/mkdir.sh

/etc/crontab和/etc/cron.d/*配置文件

/etc/crontab为系统crontab配置文件,通常只能被root用户和守护进程用来设置系统类的cron任务, 所有linux用户必须使用crontab来创建,编辑cron任务。/var/spool/cron和/var/cron/tabs为普通用户crontab配置文件。

默认cron配置文件/etc/crontab

普通/etc/crontab文件样本: 

SHELL=/bin/bash P
ATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly

由此可以看出, crontab已经预定义好了特定周期的脚本存放路径,如果符合每时、每日、每周或每月运行,可以不用配置,只需将脚本直接放入指定的文件夹下,然后 chmod +x filename,即可。 以下为目录说明:

目录                  描述
/etc/cron.d/         所有该目录下的脚本通过设置crontab来调用
/etc/cron.daily/     每天运行一次该目录下的所有脚本
/etc/cron.hourly/    每小时运行一次该目录下的所有脚本
/etc/cron.monthly/   每个月运行一次该目录下的所有脚本
/etc/cron.weekly/    每周运行一次该目录下的所有脚本

使用示例:

1. 编写脚本:采集linux系统性能指标的脚本

#!/bin/bash
cd /home/pppset/performanceLog/

#create dir for today
today=`date "+%Y%m%d"`

if [ ! -d $today ]; then
    mkdir $today; 
fi

cd $today 

#declare var to remember current hour.
hour=`date "+%H"`

echo "current hour is: "$hour

postfix="_nohup.log"

filenameOfDstat=$hour"_"$today"_dstat"$postfix".csv"
echo $filenameOfDstat

filenameOfIostat=$hour"_"$today"_iostat"$postfix
echo $filenameOfIostat

filenameOfPidstat=$hour"_"$today"_pidstat"$postfix
echo $filenameOfPidstat

filenameOfFree=$hour"_"$today"_free"$postfix
echo $filenameOfFree

filenameOfMemInfo=$hour"_"$today"_meminfo"$postfix
echo $filenameOfMemInfo

filenameOfUptime=$hour"_"$today"_Uptime"$postfix
echo $filenameOfUptime

filenameOfMpstat=$hour"_"$today"_mpstat"$postfix
echo $filenameOfMpstat

filenameOfIOtop=$hour"_"$today"_iotop"$postfix
echo $filenameOfIOtop

filenameOfSar=$hour"_"$today"_sar"$postfix
echo $filenameOfSar


#the process id which we are intrested in.: mysqld  ruby.bin
pidMysqld=`ps -e| grep mysqld.bin|awk 'NR==1 {print $1}'`
pidRuby=`ps -e| grep ruby.bin|awk 'NR==1 {print $1}'`

export DSTAT_MYSQL_USER='root'
export DSTAT_MYSQL_PWD='xxxx'

nohup  dstat -t --mysql5-cmds --mysql5-io --mysql5-keys $@ -df --disk-util --disk --mem --proc --top-cpu --top-latency  --top-bio --io --sys --filesystem --tcp --vm --output $filenameOfDstat 1 3600 &
nohup  pidstat -p $pidMysqld -u -d -w -h 2 1800 > $filenameOfPidstat &
nohup  mpstat -P ALL 2 1800 > $filenameOfMpstat &
nohup  iotop -p $pidMysqld -n 1800 -d 2  > $filenameOfIOtop &
nohup  iostat -dxk 2 1800 > $filenameOfIostat &
#nohup   sar -o $filenameOfSar  2 1800 &


#nohup   free > $filenameOfFree &
#nohup   cat /proc/meminfo > $filenameOfMemInfo &
#nohup   uptime > $filenameOfUptime &


# in every day at april ,run the shell script at 1 min past each hour.
#1 * * 4 * /root/shift_my_times.sh

#1 * * 4 * /home/pppset/performanceLog/collectLog.sh

其中关于dstat采集监控mysql io 性能的使用说明请参见这里

2. 加入定时任务:使用crontab -e 命令编辑定时任务,将以下放入到末尾。

1 0,4,10-23 * 4 * /home/pset/performanceLog/collectLogs.sh

参考链接:

1. http://zh.wikipedia.org/wiki/Cron

2. http://www.zhetenger.com/cron%E9%AB%98%E7%BA%A7%E4%BD%BF%E7%94%A8%E6%95%99%E7%A8%8B

3. 

原文地址:https://www.cnblogs.com/ToDoToTry/p/4391241.html