Linux 环境使用定时任务执行shell脚本

前言:Linux添加定时任务需要依赖crond服务,如果没有该服务,需要先安装:yum -y install crontabs

1、crond服务相关命令介绍

    启动crond服务:service crond start

    停止crond服务:service crond stop

    重启crond服务:service crond restart

    重载crond服务配置:service crond reload

    查看crond服务状态:service crond status

    查看定时任务执行状态:tailf /var/log/cron

    查看定时任务配置:crontab -l

    修改定时任务配置:crontab -e

2、定时任务相关配置

2.1、定时任务配置格式

[root@test ~]# vim /etc/crontab
SHELL=/bin/bash #执行命令的解释器
PATH=/sbin:/bin:/usr/sbin:/usr/bin #环境变量
MAILTO=root #邮件发给谁

[root@test ~]# vim /etc/crontab
SHELL=/bin/bash                     #执行命令的解释器
PATH=/sbin:/bin:/usr/sbin:/usr/bin  #环境变量
MAILTO=root                         #邮件发给谁
# 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  #星期
# |  |  |  |  |
# *  *  *  *  *   command to be executed
 
# *  表示任意的(分、时、日、月、周)时间都执行
# -  表示一个时间范围段, 如5-7点
# ,  表示分隔时段, 如6,0,4表示周六、日、四
# /1 表示每隔n单位时间, 如*/10 每10分钟

在这里插入图片描述

2.2、配置举例

00 02 * * * ls      #每天的凌晨2点整执行
00 02 1 * * ls      #每月的1日的凌晨2点整执行
00 02 14 2 * ls     #每年的2月14日凌晨2点执行
00 02 * * 7 ls      #每周天的凌晨2点整执行
00 02 * 6 5 ls      #每年的6月周五凌晨2点执行
00 02 14 * 7 ls     #每月14日或每周日的凌晨2点都执行
00 02 14 2 7 ls     #每年的2月14日或每年2月的周天的凌晨2点执行   
*/10  02 * * * ls   #每天凌晨2点,每隔10分钟执行一次
* * * * *  ls       #每分钟都执行
00 00 14 2 *  ls    #每年2月14日的凌晨执行命令 
*/5 * * * *  ls     #每隔5分钟执行一次
00 02 * 1,5,8 * ls  #每年的1月5月8月凌晨2点执行
00 02 1-8 * *  ls    #每月1号到8号凌晨2点执行
0 21 * * * ls       #每天晚上21:00执行
45 4 1,10,22 * * ls #每月1、10、22日的4:45执行
45 4 1-10 * * l     #每月1到10日的4:45执行
3,15 8-11 */2 * * ls #每隔两天的上午8点到11点的第3和第15分钟执行
0 23-7/1 * * * ls   #晚上11点到早上7点之间,每隔一小时执行
15 21 * * 1-5 ls    #周一到周五每天晚上21:15执行

3、用定时任务执行shell脚本

 */1 * * * * cd /opt/app/test/bin/ && ./task_test.sh

猜你喜欢

转载自blog.csdn.net/qq_34041723/article/details/131006124