1.创建并进入文件夹

mkdir /home/datacenter/ cd /home/datacenter/

2.创建脚本文件

touch restart.sh

3.编辑脚本文件

vim restart.sh

重启脚本

#! /bin/bash
time=$(date "+%Y-%m-%d %H:%M:%S")
echo $time>>/home/datacenterBash/restart.log 
echo "重启Docker容器:datacenter" >>/home/datacenterBash/restart.log docker restart datacenter 
echo "重启命令执行状态:"$?>>/home/datacenterBash/restart.log 
if [ $? -eq 0 ]; then
		echo "执行成功! " >> /home/datacenterBash/restart.log 
else 
		echo "执行失败! " >> /home/datacenterBash/restart.log 
		exit 
fi 
echo "重启Docker容器:datacenter完毕!输出日志在/home/datacenterBash/restart.log中">>/home/datacenterBash/restart.log

4.给予restart.sh文件执行权限

chmod u+x restart.sh

5.编辑crontab文件加入定时任务

crontab -e

6.编辑脚本执行周期

//每分钟执行 */1 * * * * /home/datacenter/restart.sh 
//每天晚上1:00执行 0 1 * * *

7.重载crontab文件

systemctl reload crond.service

8.可利用命令行crontab -l 查看定时任务

9.在输出日志中查看定时任务的执行情况vim restart.log

10.crond 命令

1.启动、重启crond服务

//启动 service crond start 
//重启 service crond restart

2.查看crond状态

service crond status

3. 停止crond服务

service crond stop