|
| 1 | +# Supercronic # |
| 2 | + |
| 3 | +Supercronic is a crontab-compatible job runner, designed specifically to run in |
| 4 | +containers. |
| 5 | + |
| 6 | + |
| 7 | +## Why Supercronic? ## |
| 8 | + |
| 9 | +Crontabs are the lingua franca of job scheduling, but typical server cron |
| 10 | +implementations are ill-suited for container environments: |
| 11 | + |
| 12 | +- They purge their environment before starting jobs. This is an important |
| 13 | + security feature in multi-user systems, but it breaks a fundamental |
| 14 | + configuration mechanism for containers. |
| 15 | +- They capture the output from the jobs they run, and often either want to |
| 16 | + email this output or simply discard it. In a containerized environment, |
| 17 | + logging task output and errors to `stdout` / `stderr` is often easier to work |
| 18 | + with. |
| 19 | +- They often don't respond gracefully to `SIGINT` / `SIGTERM`, and may leave |
| 20 | + running jobs orphaned when signaled. Again, this makes sense in a server |
| 21 | + environment where `init` will handle the orphan jobs and Cron isn't restarted |
| 22 | + often anyway, but it's inappropriate in a container environment as it'll |
| 23 | + result in jobs being forcefully terminated (i.e. `SIGKILL`'ed) when the |
| 24 | + container exits. |
| 25 | +- They often try to send their logs to syslog. This conveniently provides |
| 26 | + centralized logging when a syslog server is running, but with containers, |
| 27 | + simply logging to `stdout` or `stderr` is preferred. |
| 28 | + |
| 29 | +Finally, they are often very quiet, which makes the above issues difficult to |
| 30 | +understand or debug! |
| 31 | + |
| 32 | +The list could go on, but the fundamental takeaway is this: unlike typical |
| 33 | +server cron implementations, Supercronic tries very hard to do exactly what you |
| 34 | +expect from running `cron` in a container: |
| 35 | + |
| 36 | +- Your environment variables are available in jobs. |
| 37 | +- Job output is logged to `stdout` / `stderr`. |
| 38 | +- `SIGTERM` (or `SIGINT`, which you can deliver via CTRL+C when used |
| 39 | + interactively) triggers a graceful shutdown |
| 40 | +- Job return codes and schedules are also logged to `stdout` / `stderr`. |
| 41 | + |
| 42 | +## How does it work? ## |
| 43 | + |
| 44 | +- Install Supercronic (see below). |
| 45 | +- Point it at a crontab: `supercronic CRONTAB`. |
| 46 | +- You're done! |
| 47 | + |
| 48 | + |
| 49 | +### Installation |
| 50 | + |
| 51 | +- If you have a `go` toolchain available: `go install github.com/aptible/supercronic` |
| 52 | +- TODO: Docker installation instructions / packaging. |
| 53 | + |
| 54 | + |
| 55 | +## Crontab format ## |
| 56 | + |
| 57 | +Broadly speaking, Supercronic tries to process crontabs just like Vixie cron |
| 58 | +does. In most cases, it should be compatible with your existing crontab. |
| 59 | + |
| 60 | +There are, however, a few exceptions: |
| 61 | + |
| 62 | +- First, Supercronic supports second-resolution schedules: under the hood, |
| 63 | + Supercronic uses [the `cronexpr` package][cronexpr], so refer to its |
| 64 | + documentation to know exactly what you can do. |
| 65 | +- Second, Supercronic does not support changing users when running tasks. |
| 66 | + Again, this is something that hardly makes sense in a cron environment. This |
| 67 | + means that setting `USER` in your crontab won't have any effect. |
| 68 | + |
| 69 | +Here's an example crontab: |
| 70 | + |
| 71 | +``` |
| 72 | +# Run every minute |
| 73 | +*/1 * * * * echo "hello" |
| 74 | +
|
| 75 | +# Run every 2 seconds |
| 76 | +*/2 * * * * * * ls 2>/dev/null |
| 77 | +``` |
| 78 | + |
| 79 | + |
| 80 | +## Environment variables ## |
| 81 | + |
| 82 | +Just like regular cron, Supercronic lets you specify environment variables in |
| 83 | +your crontab using a `KEY=VALUE` syntax. |
| 84 | + |
| 85 | +However, this is only here for compatibility with existing crontabs, and using |
| 86 | +this feature is generally **not recommended** when using Supercronic. |
| 87 | + |
| 88 | +Indeed, Supercronic does not wipe your environment before running jobs, so if |
| 89 | +you need environment variables to be available when your jobs run, just set |
| 90 | +them before starting Supercronic itself, and your jobs will inherit them |
| 91 | +(unless you've used cron before, this is exactly what you expect). |
| 92 | + |
| 93 | +For example, if you're using Docker, Supercronic |
| 94 | + |
| 95 | + |
| 96 | +## Logging ## |
| 97 | + |
| 98 | +Supercronic provides rich logging, and will let you know exactly what command |
| 99 | +triggered a given message. Here's an example: |
| 100 | + |
| 101 | +``` |
| 102 | +$ cat ./my-crontab |
| 103 | +*/5 * * * * * * echo "hello from Supercronic" |
| 104 | +
|
| 105 | +$ ./supercronic ./my-crontab |
| 106 | +INFO[2017-07-10T19:40:44+02:00] read crontab: ./my-crontab |
| 107 | +INFO[2017-07-10T19:40:50+02:00] starting iteration=0 job.command="echo "hello from Supercronic"" job.position=0 job.schedule="*/5 * * * * * *" |
| 108 | +INFO[2017-07-10T19:40:50+02:00] hello from Supercronic channel=stdout iteration=0 job.command="echo "hello from Supercronic"" job.position=0 job.schedule="*/5 * * * * * *" |
| 109 | +INFO[2017-07-10T19:40:50+02:00] job succeeded iteration=0 job.command="echo "hello from Supercronic"" job.position=0 job.schedule="*/5 * * * * * *" |
| 110 | +INFO[2017-07-10T19:40:55+02:00] starting iteration=1 job.command="echo "hello from Supercronic"" job.position=0 job.schedule="*/5 * * * * * *" |
| 111 | +INFO[2017-07-10T19:40:55+02:00] hello from Supercronic channel=stdout iteration=1 job.command="echo "hello from Supercronic"" job.position=0 job.schedule="*/5 * * * * * *" |
| 112 | +INFO[2017-07-10T19:40:55+02:00] job succeeded iteration=1 job.command="echo "hello from Supercronic"" job.position=0 job.schedule="*/5 * * * * * *" |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | +## Debugging ## |
| 117 | + |
| 118 | +If your jobs aren't running, or you'd simply like to double-check your crontab |
| 119 | +syntax, pass the `-debug` flag for more verbose logging: |
| 120 | + |
| 121 | +``` |
| 122 | +$ ./supercronic -debug ./my-crontab |
| 123 | +INFO[2017-07-10T19:43:51+02:00] read crontab: ./my-crontab |
| 124 | +DEBU[2017-07-10T19:43:51+02:00] try parse(7): */5 * * * * * * echo "hello from Supercronic"[0:15] = */5 * * * * * * |
| 125 | +DEBU[2017-07-10T19:43:51+02:00] job will run next at 2017-07-10 19:44:00 +0200 CEST job.command="echo "hello from Supercronic"" job.position=0 job.schedule="*/5 * * * * * *" |
| 126 | +``` |
| 127 | + |
| 128 | + |
| 129 | +## Duplicate Jobs ## |
| 130 | + |
| 131 | +Supercronic will wait for a given job to finish before that job is scheduled |
| 132 | +again (some cron implementations do this, others don't). If a job is falling |
| 133 | +behind schedule (i.e. it's taking too long to finish), Supercronic will warn |
| 134 | +you. |
| 135 | + |
| 136 | +Here is an example: |
| 137 | + |
| 138 | +``` |
| 139 | +# Sleep for 2 seconds every second. This will take too long. |
| 140 | +* * * * * * * sleep 2 |
| 141 | +
|
| 142 | +$ ./supercronic ./my-crontab |
| 143 | +INFO[2017-07-11T12:24:25+02:00] read crontab: foo |
| 144 | +INFO[2017-07-11T12:24:27+02:00] starting iteration=0 job.command="sleep 2" job.position=0 job.schedule="* * * * * * *" |
| 145 | +INFO[2017-07-11T12:24:29+02:00] job succeeded iteration=0 job.command="sleep 2" job.position=0 job.schedule="* * * * * * *" |
| 146 | +WARN[2017-07-11T12:24:29+02:00] job took too long to run: it should have started 1.009438854s ago job.command="sleep 2" job.position=0 job.schedule="* * * * * * *" |
| 147 | +INFO[2017-07-11T12:24:30+02:00] starting iteration=1 job.command="sleep 2" job.position=0 job.schedule="* * * * * * *" |
| 148 | +INFO[2017-07-11T12:24:32+02:00] job succeeded iteration=1 job.command="sleep 2" job.position=0 job.schedule="* * * * * * *" |
| 149 | +WARN[2017-07-11T12:24:32+02:00] job took too long to run: it should have started 1.014474099s ago job.command="sleep 2" job.position=0 job.schedule="* * * * * * *" |
| 150 | +``` |
| 151 | + |
| 152 | + [cronexpr]: https://github.com/gorhill/cronexpr |
0 commit comments