Created
July 14, 2016 16:00
-
-
Save twang2218/b0253b0d807190798120f87d2cbc0ba6 to your computer and use it in GitHub Desktop.
Docker cron example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* * * * * root /app/task.py >> /var/log/task.log 2>&1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.5.2 | |
ENV TZ=Asia/Shanghai | |
RUN apt-get update \ | |
&& apt-get install -y cron \ | |
&& apt-get autoremove -y | |
COPY ./cronpy /etc/cron.d/cronpy | |
CMD ["cron", "-f"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
function build { | |
docker build -t cronjob:latest $@ . | |
} | |
function up { | |
docker run $@ \ | |
--name cronjob \ | |
-d \ | |
-v $(pwd)/task.py:/app/task.py \ | |
cronjob:latest | |
} | |
function down { | |
docker rm -f -v cronjob | |
} | |
function log { | |
docker logs cronjob $@ | |
} | |
function sh { | |
docker exec $@ -it cronjob bash | |
} | |
function main { | |
Command=$1 | |
shift | |
case "${Command}" in | |
build) build $@ ;; | |
up) up $@ ;; | |
down) down $@ ;; | |
log) log $@ ;; | |
sh) sh $@ ;; | |
*) echo "Usage: $0 {build|up|down|log|sh}" ;; | |
esac | |
} | |
main $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
from datetime import datetime | |
print("Cron job has run at {0} with environment variable ".format(str(datetime.now()))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment