Merge pull request #9 from schaal/path-unit

Add path unit to monitor changes to crontab files.
This commit is contained in:
Konstantin Stepanov 2014-07-20 19:48:53 +03:00
commit 7787ec47ab
3 changed files with 39 additions and 15 deletions

View File

@ -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:

View File

@ -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)

View File

@ -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