Skip to content

Instantly share code, notes, and snippets.

@twang2218
Created July 14, 2016 16:00
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save twang2218/b0253b0d807190798120f87d2cbc0ba6 to your computer and use it in GitHub Desktop.
Save twang2218/b0253b0d807190798120f87d2cbc0ba6 to your computer and use it in GitHub Desktop.
Docker cron example
* * * * * root /app/task.py >> /var/log/task.log 2>&1
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"]
#!/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 $@
#!/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