crontab + docker + django 部署

服务器上的启动脚本

#!/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 <

遇到的问题

在上面使用的是django custom command,虽然手动在容器能执行,本地调试也能执行,但是加入到crontab之后没有执行。最终确定是头部的引用出了问题,将默认的#!/usr/bin/env python改为#!/usr/local/bin/python2.7;并把脚本中的python .../manage.py ...改为.../manage.py ...

你可能感兴趣的:(crontab + docker + django 部署)