(1)docker容器里: vim /opt/autoDelLog.sh
内容:
#!/bin/bash find /usr/local/tomcat/logs/ -mtime +3 -name "*.log" -exec rm -rf {} \; find /usr/local/tomcat/logs/ -mtime +3 -name "*.txt" -exec rm -rf {} \;
保存,加权限 chmod 775 /opt/autoDelLog.sh
注意:#!/bin/bash不要忘记写,否则外面调用时会报错:starting container process caused "exec format error"
(2)退出容器,执行 docker exec -it docker_tomcat /opt/autoDelLog.sh docker_tomcat是我的容器名字
测试OK
(3)主机增加脚本: vim /opt/autoDelDockerLog.sh
内容就写: /usr/bin/docker exec -i docker_tomcat /opt/autoDelLog.sh 千万不要写-it,会报错the input device is not a TTY,你要不搞日志都不知道咋回事
保存,加权限 chmod 775 /opt/autoDelDockerLog.sh
(4)增加定时任务 crontab -e 内容: 10 0 * * * /opt/autoDelDockerLog.sh >/dev/null 2>&1 每天0点10分,执行/opt/autoDelDockerLog.sh脚本
|