-
-
Save jahentao/c55779544466b70f29e79b810ba32e1d to your computer and use it in GitHub Desktop.
Docker cron example
This file contains hidden or 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 hidden or 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 hidden or 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 | |
build() { | |
docker build -t cronjob:latest $@ . | |
} | |
up() { | |
docker run $@ \ | |
--name cronjob \ | |
-d \ | |
-v $(pwd)/task.py:/app/task.py \ | |
cronjob:latest | |
} | |
down() { | |
docker rm -f -v cronjob | |
} | |
log() { | |
docker logs cronjob $@ | |
} | |
sh() { | |
docker exec $@ -it cronjob bash | |
} | |
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 hidden or 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
remember to give
task.py
execution priviledge