The adoption rate of Docker has been phenomenal, and in my opinion is becoming the de-facto method for packaging and distributing applications on Linux. However there is one area that is still more difficult than I'd like - running programs on a schedule.
Historically the answer in UNIX environments has been to use
cron. Indeed, there are a variety of guides on the web for running
cron in a container.
cron is not suited to being containerised, however:
- It requires configuration files to specify what jobs to run
- It records its output to log files
- It expects to run as a demon process
cron containers seek to wrap up cron to satisfy the above, but it's complicated for what we actually want with a container, which is simply:
- Execute a program repeatedly on a schedule
- Print all output to the console
That isn't too much to ask, is it?
To that end, I wrote a very short program in Go called
mantra. It uses a slightly modified version of the excellent robfig/cron package, and allows you to simply specify the schedule that you want and the program you want to run on the command line. Standard output and standard error are shared between
mantra and the process it runs, which makes capturing output by Docker a breeze.
Here's a simple example - echo a string every 10 seconds (this assumes
echo is available as a binary on your system):
mantra "*/10 * * * * *" echo "Hello, mantra"
I've released mantra as version 0.0.1, and I've love any and all feedback. Raise an issue in the Github repository if you have any problems or questions.