👋 欢迎来到吴老二技术博客,专注运维 / 数据库 / 云原生
首页/运维服务/正文

定时任务crond的基本使用

吴
吴老二
2020-01-15运维服务4776 次浏览0 条评论
我们在运维的工作中遇到了很多重复性的工作,比如日志的自动删除,日志的自动分割,系统时间的自动更新,节点的自动删除等等操作。对于这种重复性的操作如果每次都用技术人员来操作,如果节点少很好,节点多这种重复性的操作,随他可以做批量处理,但是批量的去执行某一个操作会浪费机器的资源,有时候还会出错。这个时候我们就需要用到了crontab,定时任务。crontab只需要在需要执行定时任务的机器上写一个执行脚本,然后写到crontab里,加载一下crontab即可,根据crontab设置的时间就会自动执行脚本,我们先看一下crontab的基本命令吧,系统默认的crontab,我们目前需要知道的命令就是重启热加载
[root@wulaoer ~]# systemctl stop crond          #关闭crond服务
[root@wulaoer ~]# systemctl start crond         #开启crond服务 
[root@wulaoer ~]# systemctl restart crond       #重启crond服务
[root@wulaoer ~]# systemctl reload crond        #热加载crond服务
[root@wulaoer ~]# systemctl status crond        #查看crond服务状态
● crond.service - Command Scheduler
   Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled)
   Active: active (running) since 一 2020-01-13 10:24:50 CST; 19s ago
  Process: 14010 ExecReload=/bin/kill -HUP $MAINPID (code=exited, status=0/SUCCESS)
 Main PID: 14002 (crond)
   CGroup: /system.slice/crond.service
		   └─14002 /usr/sbin/crond -n

1月 13 10:24:50 wulaoer systemd[1]: Stopped Command Scheduler.
1月 13 10:24:50 wulaoer systemd[1]: Started Command Scheduler.
1月 13 10:24:50 wulaoer crond[14002]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 46% if used.)
1月 13 10:24:50 wulaoer crond[14002]: (CRON) INFO (running with inotify support)
1月 13 10:24:50 wulaoer crond[14002]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
1月 13 10:25:01 wulaoer systemd[1]: Reloading Command Scheduler.
1月 13 10:25:01 wulaoer crond[14002]: (CRON) INFO (running with inotify support)
1月 13 10:25:01 wulaoer systemd[1]: Reloaded Command Scheduler.
我们一般在操作定时任务时,不会管理是哪个文件,直接在crond里添加,下面说一下crond的定义方法:
[root@wulaoer ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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  #以周为单位,最小单位是星期几
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
下面我们根据上面的几个定时时间举个例子: 每天早上六点执行
0 6 * * * echo "hello, wulaoer." >> /tmp/test.txt
每隔两个小时执行
0 */2 * * * echo "hello, wulaoer." >> /tmp/test.txt
每晚11点到第二天早上8点之间,每两个小时和早上8点执行
0 23-7/2,8 * * * echo "hello, wulaoer." >> /tmp/test.txt
每个月的4号和每周的周一到周三的早上11点
0 11 4 * 1-3 echo "hello, wulaoer." >> /tmp/test.txt
1月1日早上4点
0 4 1 1 *  echo "hello, wulaoer." >> /tmp/test.txt
每天早上四点零二执行/opt/sh/log.sh
02 4 * * * bash /opt/sh/log.sh
每星期执行/opt/sh/logs.sh
22 4 * * 0 bash /opt/sh/log.sh
每天下午4点,5点,6点的5分,15分,25分,35分,45分,55分执行
5,15,25,35,45,55 16,17,18 * * *  echo "hello, wulaoer." >> /tmp/test.txt
每周一,三,五的下午三点重启系统
00 15 * * 1,3,5  shutdown -r +5
每小时的10分,40分执行
10,40 * * * * echo "hello, wulaoer." >> /tmp/test.txt
每小时的1分在执行
1 * * * * echo "hello, wulaoer." >> /tmp/test.txt
每天早晨三点二十执行
20 3 * * * echo "hello, wulaoer." >> /tmp/test.txt
每年的一月,四月,4号到9号的3点12和3点55执行
12,55 3 4-9 1,4 * echo "hello, wulaoer." >> /tmp/test.txt
上面是添加的语句,下面说一下添加的方法,我们一般是在
[root@wulaoer ~]# crontab -e
和使用vim命令一样直接把上面的定时任务添加进去即可,最后要reload一下,或者重启一下。
文章底部广告位在后台「广告设置」填写广告代码后显示

评论 0

评论需 登录 后发表(注册并验证邮箱)。
还没有评论,来抢沙发~