Periodically run a shell command on host machine on a cron schedule.
7.7K
Periodically run a shell command on host machine on a cron schedule.
This is primarily for host systems that don't have access to their own cron, or where the native cron is unreliable (like QNAP NAS's).
This container uses the pre-configured crond schedules in BusyBox to run its jobs (as discovered in this gist comment)
bash-5.0# crontab -l
# do daily/weekly/monthly maintenance
# min hour day month weekday command
*/15 * * * * run-parts /etc/periodic/15min
0 * * * * run-parts /etc/periodic/hourly
0 2 * * * run-parts /etc/periodic/daily
0 3 * * 6 run-parts /etc/periodic/weekly
0 5 1 * * run-parts /etc/periodic/monthly
Handy if you just want to run a command at generic timeframes, and not too fussed at exactly when the command is run (e.g. for backup operations).
docker run \
--name=periodic-runner \
--hostname=prunner \
-e HOST_USERNAME="username" \
-e COMMAND_DAILY="sh run_backup.sh" \
-e COMMAND_HOURLY="ping www.example.com" \
-v <path to ssh keys>:/root/.ssh \
-d \
bnutz/periodic-runner
This will set up a container that will SSH back into its own host system as username at the following periods:
www.example.com| Default Parameters | Function |
|---|---|
-e HOST_USERNAME="" | Run commands as this user on the host machine. |
-e COMMAND_15MIN="" | (Optional) Run this command every 15 mins. |
-e COMMAND_HOURLY="" | (Optional) Run this command every hour. |
-e COMMAND_DAILY="" | (Optional) Run this command every day. |
-e COMMAND_WEEKLY="" | (Optional) Run this command every week. |
-e COMMAND_MONTHLY="" | (Optional) Run this command every month. |
-e REMOTE_ADDRESS="" | (Optional) If set, will try to connect and run command on this address instead of the container's own Docker host. |
-v /path/to/ssh/keys:/root/.ssh | Path to ssh keys for container to authenticate into host with. |
COMMAND_* parameter can be left blank to not use that time interval.The first time the container is run, you will need to generate the SSH keys that it will use to authenticate back into the host machine. This will be saved to the mapped SSH keys folder to allow the auth to continue working between container updates.
docker exec -it periodic-runner ssh-keygen
docker exec -it periodic-runner ./setup_key.sh
docker exec -it periodic-runner ./run_command.sh "echo hello >> ~/test.txt"
ls -l ~/
test.txt greeting file now should appear in the home folder of your host machine.Content type
Image
Digest
Size
4.9 MB
Last updated
almost 6 years ago
docker pull bnutz/periodic-runner