centos 7 crontab定时器及开机自启

crontab 建议直接采用centos7 自带得cronteb包,
定时器:就是定时去执行干某一件事
在这里我以脚本为媒介
1 当然是创建一个脚本,创建目录随意但是你一定要记得。。
直接通过vi来创建一个test.sh脚本!
vi /etc/test.sh
编辑脚本内容:

#!/bin/bash
# chkconfig: 2345 10 90 
# description: test 
2345表示系统运行级别是234或者5时都启动此服务,20,是启动的优先级,80是关闭的优先级,如果启动优先级配置的数太小时如0时,则有可能启动不成功,因为此时可能其依赖的网络服务还没有启动,从而导致自启动失败

命令                              参数and执行文件                                 日志输出位置
/home/jdk1.8.0_161/bin/java -jar /home/jdk1.8.0_161/java/lilu.jar >> /home/log/test.log

在这里多插入点知识就是开机运行自己写的脚本,
开机运行自己的脚本配置:
1,创建一个test.sh脚本,
mkdir /etc/test.sh
给该文件可执行权限
chmod +x /etc/test.sh
2,在开机加载的配置项中添加test.sh脚本
vi etc/rc.d/rc.local
在其中添加创建好脚本的位置 /etc/test.sh
然后再给该文件可执行权限
chmod +x /etc/rc.d/rc.local
这样就可以在开机的时候执行该脚本了,以后就可以再rc.local中添加服务就是了

如果开机启动未成功可以查看chkconfig命令

好了,言归正传 :
脚本我们已经创建好了然后就只需要配置在crontab中就可以了↓↓
vi /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
这个就是定时任务 每天晚上2030执行test.sh脚本
30 20  *  *  * /etc/test.sh

当我们编辑好定时任务后记得保存:wq
得装载一遍才生效哦crontab /etc/crontab
crontab -l查看任务
crontab -e便捷定时编辑任务

当我们编辑完定时任务过后,记得重载一遍crond
systemctl reload crond.service

启动、停止、重启服务↓
systemctl start crond.service

systemctl stop crond.service

systemctl restart crond.service

为了方便我们可以把crontab设置成开机自启 在 /etc/rc.d/rc.local 脚本中加入 systemctl start crond.service即可

猜你喜欢

转载自blog.csdn.net/weixin_41558061/article/details/80268639
今日推荐