r/docker • u/kwhali • May 28 '26
How to approach running a background command at a repeated interval?
Without Docker involved, I'd quite happily use systemd-timers tbh... but to my surprise there doesn't appear to be any well-established standalone alternative program for running a command at a recurring interval? (unless it aligns with the cron form of scheduling)
What I'm familiar with:
Cron (Vixie) is commonly chosen but:
- Main concern: Running a task every 20 minutes is scheduled relative to the hour, not from when the container starts... so if you started the container at 2:32, it'll run in 8 minutes. This is worse if the interval was something like 25 minutes (runs at
00,25,50minute of each hour, such that50=>00is 10 minutes instead of expected 25 minute interval). If you want the command to run at container startup, then followed by the interval, cron isn't really ideal. - There are other commonly known caveats (for which
supercronicmostly addresses), but this post is long enough (different concerns from what I want to focus on).
- Main concern: Running a task every 20 minutes is scheduled relative to the hour, not from when the container starts... so if you started the container at 2:32, it'll run in 8 minutes. This is worse if the interval was something like 25 minutes (runs at
- Systemd Timers are fantastic but you can't really use those with Docker (Podman and perhaps other runtimes are valid, but for a public image Docker compatibility is important).
- Naive wrapper scripts like bash with a
whileloop paired withsleepcan work, but needs care under some contexts that add complexity (supercronicforwards signals to the command, systemd-timers do too I think?) - Ofelia (uses cron scheduling behaviour) is typically intended not to be bundled into individual container images for local use, but rather leveraging the Docker API socket.
Typically container images are designed to run a single logical service (which may involve one or more processes depending on software, usually the entrypoint is minimal), but a logical service may involve several different components which can benefit from bundling into a single image (rather than added complexity/config externalized to unify several images).
- Some readers here may largely disagree with the practice, but there are valid use-cases that warrant it, and I'd rather not derail the discussion on this topic.
- In my case the image uses supervisord as an entrypoint to run the main process alongside an internal recurring task (command).
7
u/No_Lifeguard7725 May 28 '26
Describe what is the end goal please.
2
u/randomusername0O1 May 28 '26
Unsure of specific constrains you may have, but, I do exactly this as part of our platform.
We combine python-rq with rq-scheduler.
Rq scheduler allows you to define increments eg 25 minutes or use cron format.
I can confirm when using increments, it's based on container start time.
It may introduce too many dependencies for your use case, though it does meet your timing requirements.
1
u/alchatti May 28 '26
I have been exploring s6-overlay for such scenario, another way to achieve something like that would be a web api that triggers the job and then you can setup a calling mechanism from another container as a heart beat and have that as dependency.
Kubernetes have the concept of job container. Using entrypoint your container can excute a command or start a service if no command was supplied during the call.
1
u/zoredache May 28 '26
It really depends on your requirements, but one option is to use something like cronicle github.com/cronicle-edge/cronicle-edge, or semaphoreui semaphoreui.com/.
Basically how this could work would be to add a ssh key to those 'cron' containers that allow them to ssh to the host, and execute a script that does
docker exec container some_command. Your authorized keys could use force_command.I think 95% of the time people looking wouldn't be looking for a 'cron' like scheduling to also handle a container startup task. It seems like a pretty rarely needed requirement.
1
u/OptimalMain May 28 '26
You don’t have to set cron to a specific time.
It’s configurable for intervals, so you can set it to every 20 minutes.
The problem you explain for 25 minute intervals only exists if you create 3 cron jobs with the minutes hardcoded
1
u/sshwifty May 28 '26
Maybe I am missing something, but why not just a bash script to do this? Increment your timer on whatever intervals you want.
1
u/hezden May 28 '26
To me the most intuitive way would be to have the polling interval to be configurable in the service…
Can’t you just set a cron-job for running the command inside of the container? docker exec -it containername getmail
1
u/Procrastin8_Ball May 29 '26
Could you use at and just have your script schedule the next check then? At can you "now + time". Maybe a little fragile so a cron to check that it's not broken?
1
u/thaeli May 29 '26
Another option is to do a very frequent cron job, with a wrapper script that fast exits if the real criteria haven’t been met. For instance, run every minute:
0
u/Normal_Distance_6515 May 28 '26
Use kiosk i think it helps your request.