funnyzak/cron

By funnyzak

Updated about 1 month ago

a lightweight service that runs in the background and executes scheduled tasks.

Image

293

Cron

Docker TagsImage SizeDocker StarsDocker Pulls

cron is a lightweight service that runs in the background and executes scheduled tasks. It builds on the official alpine image and includes dcron as the cron service. The image is available for multiple architectures, including linux/386, linux/amd64, linux/arm/v6, linux/arm/v7, linux/arm64/v8, linux/ppc64le, linux/riscv64, linux/s390x.

Installed packages: dcron, ca-certificates, curl, tar, tzdata, bash, zip, unzip, rsync.

Pull the Image

# Docker Hub
docker pull funnyzak/cron:latest
# GitHub Container Registry
docker pull ghcr.io/funnyzak/cron:latest
# Aliyun Registry
docker pull registry.cn-beijing.aliyuncs.com/funnyzak/cron:latest

How to Use

Environment Variables
  • CRON_STRINGS: Crontab strings, separated by \n.
  • STARTUP_COMMANDS: Commands to run before starting the cron service.
  • EXTRA_PACKAGES: Optional, Specify extra packages to install. Default is empty. e.g. mysql-client mariadb-connector-c.

CRON_STRINGS example:

Single job:

# Run every minute
* * * * * /scripts/echo.sh >> /var/log/cron/cron.log 2>&1

Multiple jobs:

# Multiple jobs
* * * * * /scripts/request.sh >> /var/log/cron/cron.log 2>&1\n* * * * * /scripts/echo.sh >> /var/log/cron/cron.log 2>&1
Crontab Files

crontab files are located in /etc/cron.d, you can mount a volume to this directory to add your own crontab files.

For example, you can create a file named my-cron with the following content:

* * * * * /scripts/request.sh >> /var/log/cron/cron.log 2>&1

Note: Default log file should be /var/log/cron/cron.log. You should forward all script output to this file.

Usage

Using CRON_STRINGS
docker run --name="cron" -d \
  -e 'CRON_STRINGS=* * * * * echo "Hi, i am cron." >> /var/log/cron/cron.log 2>&1' \
  funnyzak/cron

docker run --name="cron1" -d \
  -e 'CRON_STRINGS=* * * * * /scripts/echo.sh >> /var/log/cron/cron.log 2>&1' \
  funnyzak/cron

docker run --name="cron2" -d \
  -e 'CRON_STRINGS=* * * * * /scripts/echo.sh >> /var/log/cron/cron.log 2>&1' \
  -e 'STARTUP_COMMANDS=echo "Hello, World!"' \
  -e 'EXTRA_PACKAGES=git' \
  funnyzak/cron

docker run --name="cron5" -d \
  -e 'CRON_STRINGS=* * * * * /scripts/request.sh >> /var/log/cron/cron.log 2>&1' \
  -e 'STARTUP_COMMANDS=/scripts/echo.sh >> /var/log/cron/cron.log 2>&1' \
  funnyzak/cron
Using Crontab Files
docker run --name="cron6" -d \
  -v ./cron/crontabs:/etc/cron.d \
  -v ./cron/scripts:/scripts \
  funnyzak/cron
Compose Example
version: '3'
services:
  cron:
    image: funnyzak/cron
    privileged: true
    container_name: cron
    environment:
      - TZ=Asia/Shanghai
      - LANG=C.UTF-8
      - CRON_TAIL=1
      - CRON_STRINGS=* * * * * /scripts/echo.sh >> /var/log/cron/cron.log 2>&1
    restart: on-failure
    volumes:
      - ./cron/scripts:/scripts
      - ./cron/crontabs:/etc/cron.d

Docker Pull Command

docker pull funnyzak/cron