linux crontab计划任务
linux crontab计划任务
crontab计划任务参数无法传递
给项目上写了个清理日志的计划任务,脚本拿出来执行能正常清理,放到crontab执行却不生效,最后发现是脚本里的参数,也就是环境变量无法传递导致,而变量无法获取是因为没用docker的绝对路径,脚本如下
#!/bin/bash
containterId=`docker ps -a| grep ecs| grep -vE "cloud|nginx|job|console" |awk '{print $1}'`
>/var/lib/docker/containers/${containterId}*/*.log
改良版,日志可以正常清理了
#!/bin/bash
clearlog() {
local containterId=`/usr/local/bin/docker ps -a| grep ecs| grep -vE "cloud|nginx|job|console" |awk '{print $1}'`
>/var/lib/docker/containers/${containterId}*/*.log
echo "start clear ${containterId} log now" >> /root/clear.log
}
clearlog
//添加计划任务
crontab -e
* */3 * * * /data/stageECS/install_ecs/crontabJob/clearEcsLog.sh