Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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