From d69d3e3016294d479e86162659d59e8f11f922c8 Mon Sep 17 00:00:00 2001 From: Daniel Schaal Date: Sun, 20 Jul 2014 14:13:47 +0200 Subject: [PATCH 1/3] Add path unit to monitor changes to crontab files. Add systemd-crontab-update.path and .service files to monitor changes to cron files and start systemd-crontab-update when necessary. --- systemd-crontab-generator | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/systemd-crontab-generator b/systemd-crontab-generator index 27f5bd9..d6f792b 100755 --- a/systemd-crontab-generator +++ b/systemd-crontab-generator @@ -233,6 +233,39 @@ ExecStart=%s -c '%s' return '%s.timer' % unit_name +def generate_path_unit(): + combinedcronfiles = [ '/etc/crontab', '/etc/cron.d', '/etc/anacrontab', '/var/spool/cron' ] + with open('%s/systemd-crontab-update.path' % (TARGER_DIR), 'w') as f: + f.write('''# Automatically generated by %s + +[Unit] +Description=[Cron] Update cron units +RefuseManualStart=true +RefuseManualStop=true + +[Path] +%s +''' % (SELF,'\n'.join([ "PathChanged="+f for f in combinedcronfiles ]) )) + + try: + os.symlink('%s/systemd-crontab-update.path' % (TARGER_DIR), '%s/systemd-crontab-update.path' % (TIMERS_DIR)) + except OSError as e: + if e.errno != os.errno.EEXIST: + raise + + with open('%s/systemd-crontab-update.service' % (TARGER_DIR), 'w') as f: + f.write('''# Automatically generated by %s + +[Unit] +Description=[Cron] Update cron units + +[Service] +Type=oneshot +ExecStart=/usr/bin/systemd-crontab-update +''' % (SELF)) + + return "systemd-crontab-update.path" + seqs = {} def count(): n = 0 @@ -262,3 +295,7 @@ for filename in USERCRONTAB_FILES: except IOError: pass +try: + generate_path_unit() +except IOError: + pass From 99b2bf041d29bedfce72c81f5271295fd29f7aff Mon Sep 17 00:00:00 2001 From: Daniel Schaal Date: Sun, 20 Jul 2014 16:58:50 +0200 Subject: [PATCH 2/3] remove update() function from crontab --- crontab | 9 --------- 1 file changed, 9 deletions(-) diff --git a/crontab b/crontab index 562e714..d4669e7 100755 --- a/crontab +++ b/crontab @@ -41,10 +41,6 @@ args_parser.add_argument('-i', '--ask', dest='ask', action='store_true', default #MLS_LEVEL setting to the crontab file before editing / replacement occurs #- see the documentation of MLS_LEVEL in crontab(5).''') -def update(): - sudo = '' if os.getuid() == 0 else '/usr/bin/sudo ' - os.system('%s/usr/bin/systemd-crontab-update' % sudo) - def confirm(message): while True: answer = raw_input(message).lower() @@ -61,7 +57,6 @@ def list(cron_file, args): def remove(cron_file, args): if not args.ask or confirm('Are you sure you want to delete %s (y/n)? ' % cron_file): os.unlink(cron_file) - update() def edit(cron_file, args): with tempfile.NamedTemporaryFile() as tmp: @@ -74,8 +69,6 @@ def edit(cron_file, args): tmp.file.seek(0) with open(cron_file, 'w') as out: out.write(tmp.file.read()) - - update() def replace(cron_file, args): infile = args.file @@ -87,8 +80,6 @@ def replace(cron_file, args): with open(cron_file, 'w'), open(infile, 'r') as out, inp: out.write(inp.read()) - update() - if __name__ == '__main__': args = args_parser.parse_args() cron_file = os.path.join(CRONTAB_DIR, args.user) From fe7580bf5de874221effa31914e72631088b94ad Mon Sep 17 00:00:00 2001 From: Daniel Schaal Date: Sun, 20 Jul 2014 17:00:08 +0200 Subject: [PATCH 3/3] Update README.md to not reference systemd-crontab-update for crontab command. --- README.md | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e5e57fe..83515a1 100644 --- a/README.md +++ b/README.md @@ -49,14 +49,10 @@ execute commands from `package()` sub. ## Usage -The generator runs on system boot. If you change your crontabs in runtime, run `systemd-crontab-update` -script as root (`sudo systemd-crontab-update`) to regenerate systemd units and reload them. +The generator runs on system boot and when the crontabs change. The project includes simple `crontab` command equivalent, which behaves like standard crontab command -(and accepts the same main options), and runs `systemd-crontab-update` command after user crontab file -update. Note, though, the systemd-crontab-update requires superuser priviledges, so `crontab` tries -to run it under `sudo`, so if you are not allowed to run `systemd-crontab-update` via `/etc/sudoers` -file, you can't update crontab timers. +(and accepts the same main options). To control cron jobs, use `cron.target`, e.g. to start and enable cron after installation: