Story 13 · GoCron
The Clock That Keeps the Chores
“A chore you must remember is a chore that will be forgotten.”
16 min read · by Syed Jafer K
1. The Four Lines Nobody Could Explain
Priya's server was no longer a lonely one. It had a bell that rang, a door that locked itself, a backup that told her when it failed, and a watchman who lived in another house. She was, for the first time in a year, a little proud of it.
Then Karthik came to dinner at the GoDisai office, which meant he came to look at her screen.
"Add one more job for me," he said. "Rotate the keys on the first of every month."
"Easy." She opened the crontab and, as she scrolled down to the bottom, stopped.
0 2 * * * /home/priya/bin/backup.sh >> /var/log/backup.log 2>&1
30 2 * * 0 /home/priya/bin/prune.sh
*/5 * * * * /home/priya/bin/watch-the-watchman.sh
15 4 1 * * /home/priya/bin/rotate-keys.sh > /dev/null 2>&1
Four lines. She had written every one of them, and she could not have said, standing there, when any of them had last run.
"Which of these ran last night?" asked Karthik.
"The backup. I have a monitor on it."
"And the prune?"
She did not answer.
"The fourth line sends its output to nowhere," he said gently. "You told it to be quiet, and it has been very obedient. If it fails, nobody will ever know. And if you want to run one by hand tonight, how do you do it?"
"I type the command."
"As which user? With which environment? From which folder?" He sat back. "A crontab is a clock with no memory. It rings, and then it forgets that it rang."
"Then what do I use?"
"Something that writes the chores down in one place, runs them, and remembers every time it did. It is called GoCron."
2. The Clock With a Notebook
GoCron is a task scheduler that you run yourself. Florian Hoss wrote it in Go, with a web page in Vue.js, and it is free under the MIT licence. You describe your chores in one YAML file. It runs them on schedule, keeps every run and its output, and shows it all on a web page.
Karthik drew it on a napkin as three words, in the way he liked to. Write down. Run. Remember.
The schedule uses the same five-field cron expressions that Priya already knew, so nothing about when would change. What would change is everything around it. A job is a name, a schedule and a list of commands. A run is one attempt at a job. Each run is kept in a small SQLite database, with its output, so that on a Tuesday morning she could open a page and read what had happened on Monday night.
"One thing I should say now, so you do not blame it later," said Karthik. "GoCron remembers what it did. It cannot tell you about what it never did. We will come to that. It is the end of the story."
3. The Door Stays Closed
Priya was ready to start it up. Karthik put one finger on the port number before she could type.
"Which of your machines can see this?"
"All of them. The whole internet, I suppose."
"This page has a button that runs commands on your server."
She took the finger off the Enter key and changed one thing.
docker run -d --restart=always \
--name gocron \
-p 127.0.0.1:8156:8156 \
-v ./config/:/app/config/ \
ghcr.io/flohoss/gocron:latest
The 127.0.0.1 in front of the port was the same habit she had learned with the watchman. The program would be reachable only from the machine itself. The -v line kept the configuration and the database in a folder she could see, so that replacing the container would never replace her chores. If she preferred Compose, the same thing was a few lines.
services:
gocron:
image: ghcr.io/flohoss/gocron:latest
restart: always
ports:
- '127.0.0.1:8156:8156'
volumes:
- ./config/:/app/config/
She opened a tunnel from her laptop, as before, and went to http://127.0.0.1:8156.
ssh -L 8156:127.0.0.1:8156 priya@main.example.org
The page was not empty. On its first start, GoCron had written a sample configuration with example jobs. She pressed the run button on one of them, and watched its output arrive on the page, line by line, while it was still running.
"That," said Priya, "is already better than my crontab."
4. Moving the Chores In
She started with the one she cared about most, the backup. She opened ./config/config.yaml, deleted the sample jobs, and wrote her own.
time_zone: 'Asia/Kolkata'
jobs:
- name: 'Nightly backup'
cron: '0 2 * * *'
timeout: '30m'
retries: 2
envs:
- key: RCLONE_REMOTE
value: 'vault'
commands:
- rclone sync /srv/data ${RCLONE_REMOTE}:data --backup-dir "${RCLONE_REMOTE}:old/$(date +%F)"
Karthik read it over her shoulder, nodding.
The time_zone line she liked best. All schedules would run in that zone, so "two in the morning" meant two in the morning for her, whatever the server thought. The timeout meant that a command which hung would be stopped after half an hour instead of sitting there until the next ice age. The retries meant that a flaky network would get two more chances, one command at a time. And the commands ran through the shell, so the pipes and the $(date +%F) worked just as they did in a terminal.
She saved the file. She did not restart anything. In the log, one line appeared saying the configuration had been reloaded, with the number of jobs it now held.
"It watches the file," said Karthik. "Most changes apply the moment you save. A few things, like the port, need a restart, and the project's README lists which. If a job happens to be running when you save, it finishes first."
The second chore was the log prune. By the third she had grown tired of typing the same lines, so she moved them up into a block of defaults that every job would inherit.
job_defaults:
timeout: '10m'
retries: 1
pre_commands:
- echo "Starting $(date)"
post_commands:
- echo "Finished $(date)"
jobs:
- name: 'Nightly backup'
cron: '0 2 * * *'
timeout: '30m'
commands:
- /home/priya/bin/backup.sh
- name: 'Prune old logs'
cron: '30 2 * * 0'
commands:
- find /var/log/app -name '*.log' -mtime +30 -delete
- name: 'Clear the cache'
disable_cron: true
commands:
- /home/priya/bin/clear-cache.sh
Every job now began and ended with a line saying so. The backup kept its own longer timeout, because a job's own value always beats the default. The last job had no schedule at all, and was marked so. It ran only when somebody pressed its button, which was exactly right for the chore she did after a bad release and never on a clock.
"If any command fails," said Karthik, "the job stops there. It does not carry on to the next line."
"Good. I do not want to prune after a backup that broke."
"Then you already agree with the default. If you ever want the opposite, you can set disable_fail_fast: true. The run is still marked as failed, but the remaining steps go ahead."
5. The Password in the File
On the fourth chore, the key rotation, she needed a token for the service she was talking to. She had it in her clipboard. She put it in the envs block, and was about to save when Karthik reached over and closed the lid halfway.
"Read me what you just typed."
"A token."
"In a file you will copy to your laptop, paste into a chat when something goes wrong, and one day commit to a repository." He did not sound angry. He sounded like someone who had done it. "The project is quite direct about this. Do not keep passwords or tokens in the config file."
"Then where?"
"Outside it. Either a file you mount in as a Docker secret, or an environment file passed to the container. Either way, the job only refers to the name, and the value is never in the YAML."
She made a file with the token in it, outside the repository, readable only by her. She passed it in with --env-file, and in the job she wrote ${ROTATE_TOKEN}. The file she would show to people now contained a name instead of a secret.
He added three quiet warnings while she worked, from the project's own safety notes. Commands in the container run as root unless you choose otherwise, so give a sensitive job the least power it needs. Mount into the container only the folders a job truly needs. And GoCron can install helper tools such as rclone, restic and rsync when it starts, but every tool is one more thing that can go wrong, so list only the ones you use.
software:
- name: 'rclone'
- name: 'rsync'
6. The Night Nothing Happened
For two weeks everything was green. Priya opened the page each morning, as one opens a window to see the weather, and found the same tidy rows of runs. She began, secretly, to enjoy it.
Then one Sunday, Meena asked her a question about the logs. Meena's learner community wrote files into a folder on the server, and she had noticed that it was larger than it had been in a long time.
"Did the Sunday clean-up run?" Meena asked.
Priya opened the page. The clean-up job was a weekly prune, set for 2:30 in the morning on Sundays. There was no run on the list for last Sunday, none for the Sunday before, and none for the Sunday before that. There was no failure and no error. The job had simply never been recorded.
She sat looking at it, with the odd feeling of looking at a gap where a tooth should be.
She called Karthik.
"What time does the backup run?" he asked.
"Two."
"How long does it take?"
She checked the history. "Forty minutes. A bit more on Sundays."
"And the prune starts at half past two."
There was a long, quiet pause on the line.
"Oh," said Priya.
"Read the failure notes in the README," said Karthik. "GoCron does one thing at a time. While any job is running, no other job starts. A run that comes due while another is busy is not queued. It is skipped."
Three Sundays in a row, the backup had been halfway through at 2:30, and the clock had arrived, found the door occupied, and gone away without a word.
She moved the prune to six in the morning. That same afternoon she went through the whole file and made sure no two chores could be running at the same time. She left a note to herself above the jobs, in a comment, so that the next person to add a line would see it.
"If you ever need two things to truly run together," said Karthik, "the project suggests a second GoCron with its own file. It is better than fighting the rule."
She asked him whether there were other rules like it. He told her about three.
If the container is down at the moment a job is due, that run is simply lost. It is not replayed afterwards, because that is how cron has always worked. Retries apply to each command and happen straight away, with no waiting between them. And daylight saving can swallow a schedule that falls inside the skipped hour, or run one twice in the repeated hour, though in Chennai that was somebody else's problem.
"Which brings us," said Karthik, "to what I promised you at the start."
7. Who Notices the Silence
"GoCron remembers every run," said Karthik. "Look at what happened. Nothing was wrong with the record. The record was perfect. It simply had nothing in it for the prune, and you had to be looking to notice."
"So I need something that notices what did not happen."
"You built one. It lives in another house."
He was right, and she knew it before he finished. In Chapter 12 she had made a push monitor for the nightly backup. The monitor does not ask the job anything. It waits to be told, and when the call does not arrive in time, it raises the alarm. A job that never started cannot call, and so it is exactly the silence that gets noticed.
GoCron can make that call by itself. It has a section called healthcheck, and in it you give three addresses, one for when a job starts, one for when it ends, and one for when it fails. The project built it with another monitor in mind, called Gatus, but the idea is the same. She gave the end call the Push URL from her watchman, and chose a gentle interval to match the chore: a day and an hour for the nightly job, a little over a week for the weekly one.
Now the two programs covered each other. If a job failed, GoCron said so, with the full output saved on the page. If a job did not even begin, the watchman noticed the missing call and sent a message to her phone, and also to her email.
"The clock remembers," said Karthik. "The watchman notices. Alone, each of them has a blind spot."
There was one more way to get a message out, which she kept for a different kind of chore. If the helper tool apprise is installed through the software list, a command in a job can send a message directly to a chat or to email. She used it on the disk-space check, which ended in exit 1 when the disk was too full. The run turned red on the page, and a message arrived on her phone before anyone had logged in.
8. A Name on the Door
By now other people wanted to see the page. Meena wanted to know about the clean-up. Karthik wanted to see the key rotation without asking. The tunnel had been fine for one person, and it was a bad way to share something.
The page needed a name, and a lock.
The name was easy. Caddy, from Chapter 3, took three lines.
chores.example.org {
reverse_proxy 127.0.0.1:8156
}
Then she told GoCron which proxy to believe. By default, GoCron uses the address of whoever is connected directly, because a forwarded address in a header can be faked by anyone. Once she listed her proxy as trusted, it could pass on the real visitor's address.
server:
trusted_proxies:
- '10.0.0.0/8'
The lock was the more important part. Without it, the page and its API are open to anyone who can reach them, and the page can run commands. GoCron can ask for a sign-in through any OpenID Connect provider, and the project tests it against one called Pocket-ID. In her provider she created a client, with a callback address that was the page's own address followed by /api/auth/callback. She copied the client's ID and secret, and then she did the careful thing. The secret went in as an environment variable, GC_AUTH_OIDC_CLIENT_SECRET, rather than into the file she had learned to keep clean.
auth:
oidc:
enabled: true
issuer_url: 'https://id.example.org'
client_id: 'gocron'
cookie_secure: true
She restarted the container, because the sign-in settings are read only at startup. She also set cookie_secure: true, since the page was now served over HTTPS. Without it, the browser quietly drops the session cookie, and you are sent back to the login page forever, which is a very confusing way to spend an afternoon.
Karthik raised one last point about the page's built-in terminal. It is limited by a list of allowed commands, and there is a setting called allow_all_commands. The documentation says, in plain words, not to turn it on in production. She did not.
9. Keeping the Clock Wound
A scheduler that only grows is a scheduler that fills a disk. GoCron deletes old runs after a number of days, set by delete_runs_after_days. The shipped example used seven. Priya chose thirty, because a month of nightly logs was small and a month was about as far back as she ever looked.
delete_runs_after_days: 30
Updating was the same few lines as everything else in the book, because the container held nothing that mattered.
docker compose pull
docker compose up -d --force-recreate
She made a copy of the config folder before updating, and sent it away encrypted, as she did with every backup now. The folder holds the jobs and the history. It is the whole system, and it fits in a few kilobytes of YAML and a small database.
It was while updating, on a Saturday at ten past two in the morning, that she learned the second rule for herself. She had pulled the new image at 2:05 and the container was down for a minute. The nightly backup was due at two, and she had started the job by hand afterwards, from the page, with a single click. It was a small pleasure to know where the button was.
10. Epilogue: The Monday After
A few weeks later, on a Monday, Karthik asked her to add a job.
She opened the configuration. She wrote six lines, saved the file, and watched the single line about the reload appear in the log. The new chore was on the page before she had finished her tea.
"Which of them ran last night?" he asked, exactly as he had once before.
She turned the screen towards him. Five chores, five green lines, and the output of each one a single click away. Further down, the key rotation showed a red line from two days earlier, and just under it, a green one, where the retry had succeeded.
"All of them," said Priya. "And I know about the one that tripped. And if one of them had not started at all, my phone would have told me before I woke up."
Karthik looked at the four old lines of the crontab, which she had left in a comment at the top of the file, as a kind of museum.
"Good," he said. "A chore that runs by itself is a gift. A chore that tells you how it went is something you can finally trust."
Quick Reference Cheat-Sheet
| I want to... | Do this |
|---|---|
| Start it | Run ghcr.io/flohoss/gocron:latest, port 8156, a volume on /app/config/ |
| Keep it private at first | Publish 127.0.0.1:8156:8156 and use an SSH tunnel |
| Add a chore | Add an entry under jobs with a name, a cron and commands |
| Run a chore now | Press its run button on the web page |
| Make a manual-only chore | Give it no cron, or set disable_cron: true |
| Stop a hung command | Set timeout |
| Try a flaky step again | Set retries (per command, immediate) |
| Share common settings | Use job_defaults |
| Run all steps even if one fails | Set disable_fail_fast: true |
| Keep secrets out of the file | An environment file or Docker secrets, then ${VAR} in commands |
| Put it behind HTTPS | A reverse proxy, plus server.trusted_proxies |
| Require a login | Enable OIDC sign-in, cookie_secure: true over HTTPS, restart |
| Learn about a job that never started | Call a Push monitor from the healthcheck end call |
| Install helper tools | The software list, then recreate the container |
| Override a setting from the environment | A GC_ variable, then restart |
| Update | Pull the image, recreate the container, keep the folder |
Key takeaways
- A scheduler is write down, run, remember. A bare crontab does the second and forgets the other two.
- GoCron runs one job at a time. A job that comes due while another is running is skipped, not queued, so keep schedules apart.
- A run missed while the container was down is not replayed.
- Keep secrets out of
config.yaml, and the lock on before the name on the door. - The scheduler remembers, the monitor notices. Use both, because a job that never started leaves nothing behind to find.
- Back up the
configfolder. It is the whole system.
A clock that rings is only half the job. The other half is somebody who notices when it does not.