Instead of writing a bash script for automating a recurring job I decided to write a general purpose job executor in Python this time.
The idea was to define recurring jobs (for backup, update checks, etc.) in a config file, which can be executed by name. The jobs will then be called via Cron or Systemd once per day (or any other interval).
Also I wanted to be informed about the outcome of some jobs. But instead of filling up my mailbox I prefer to receive the notifications on my phone via Telegram.
So I ended up writing JobMonkey 🙂 You can find the source on Github.
Jobs consist of one or more tasks. Where each task is a command to be executed.
Features:
- Configuration is stored in a YAML file.
- Jobs can be enabled or disabled.
- Job based reporting can be enabled ‘always’ or ‘on-failure’
- Reporting can also be enabled for single tasks
- Reports are sent via Telegram
The config YAML looks like this:
global:
user: root
notifications:
telegram:
bot_token: 'XXXXXXXXXX:XXX_XXXXXXXXXX_XXXXXXXXXXXXXXXXXXXX'
chat_id: '0000000000'
jobs:
- name: test
enabled: false
reports:
- type: telegram
when: on-failue
tasks:
- run: "hostname -f"
report: true
- run: "false"
report: false
- name: upgrade-check
descr: "Check for security updates"
enabled: true
reports:
- type: telegram
when: always
tasks:
- run: "apt update"
- run: "/usr/local/sbin/print_updates.py"
report: true
Usage:
# jobmonkey -h
usage: jobmonkey [-h] {list,run} ...
Jobmonkey - runs jobs and sends reports
optional arguments:
-h, --help show this help message and exit
valid actions:
{list,run}
list List jobs
run Run one or more jobs
Listing defined jobs:
# jobmonkey list +-------------------------+---------+ | Job name | Enabled | +-------------------------+---------+ | test | False | | upgrade-check | True | | backup-dump-database | True | | backup-local-disk | True | | backup-remote-backblaze | True | +-------------------------+---------+
Listing defined jobs:
# jobmonkey run upgrade-check -v
processing job: '{'name': 'upgrade-check', 'enabled': True, 'reports': [{'type': 'telegram', 'when': 'always'}], 'tasks': [{'run': 'apt update'}, {'run': '/usr/local/sbin/print_updates.py', 'report': True}]}'
processing report 'telegram'Example report:
Job upgrade-check finished with 0 error(s). Result from '/usr/local/sbin/print_updates.py': 'Security upgrades: 2 Total upgrades: 6 List of security upgrades: curl git git-man ...