crontab + docker + django 部署
2018-07-31 本文已影响74人
好小葱1
服务器上的启动脚本
#!/usr/bin/env bash
docker_name=ip-flow-task
project_path=/home/code/ip-flow-task
image=xiaochonghao/django1.11.1:v2
echo "git tag" $1
echo "project path" ${project_path}
echo "docker name" ${docker_name}
echo "image" ${image}
cd ${project_path}
git pull
git checkout $1
echo "restart ip-flow service....."
echo "ensure old service start______________________________________"
docker ps
echo 'stop and rm old service'
echo "stop docker " ${docker_name}
docker stop ${docker_name}
echo "docker rm" ${docker_name}
docker rm ${docker_name}
echo "start new service"
docker run -d --name ${docker_name} -v ${project_path}:/data/ip-flow ${image} /bin/sh -c 'sh /data/ip-flow/bin/run_task.sh test;'
docker ps
docker container 内执行的命令
#!/usr/bin/env bash
dev_mode=test
if [ $# -ge 1 ]; then
if [ $1 != 'hk' ] && [ $1 != 'jp' ] && [ $1 != 'test' ]; then
echo 'usage: sh run_task.sh [hk|jp|test]'
exit
fi
if [ $1 == 'hk' ]; then
dev_mode=_hk
elif [ $1 == 'jp' ]; then
dev_mode=_jp
else
dev_mode=""
fi
fi
echo "dev_mode=${dev_mode}"
cat > /var/spool/cron/alarm_crontab <<EOF
# 每天07:00执行一次
0 7 * * * /data/ip-flow/manage${dev_mode}.py write_flow_beat
# 每隔2个小时,在30分的时候执行一次
30 */2 * * * /data/ip-flow/manage${dev_mode}.py del_old_beat
EOF
crontab /var/spool/cron/alarm_crontab
service crond restart
chmod -R 777 /data/ip-flow/logs/
tailf /data/ip-flow/bin/run_task.sh
遇到的问题
在上面使用的是django custom command
,虽然手动在容器能执行,本地调试也能执行,但是加入到crontab
之后没有执行。最终确定是头部的引用出了问题,将默认的#!/usr/bin/env python
改为#!/usr/local/bin/python2.7
;并把脚本中的python .../manage.py ...
改为.../manage.py ...