Skip to content

Instantly share code, notes, and snippets.

@temoto
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save temoto/077bdbbc56be01a91c1a to your computer and use it in GitHub Desktop.
Save temoto/077bdbbc56be01a91c1a to your computer and use it in GitHub Desktop.
Manage cron jobs in a nice spreadsheet, export to crontab format using this script.
#!/usr/bin/env python3
'''
каждый день{tab}13:45{tab}игнорируется{tab}http://ping.url/
->
45 13 * * *{tab}/usr/bin/curl -fsS 'http://ping.url/'
'''
import re
import sys
RE_EVERY_N_MINUTES = re.compile(r'каждые (\d+) минуты?')
def convert(line):
columns = line.split('\t')
day_str = columns[0].lower()
(hour, minute) = columns[1].split(':')
cmd = columns[3]
if cmd.startswith('http'):
cmd = "/usr/bin/curl -fsS '{0}'".format(cmd)
every_match = RE_EVERY_N_MINUTES.match(day_str)
if day_str == 'каждый день':
fmt = '{m} {h} * * *\t{cmd}'
elif day_str == 'каждую субботу':
fmt = '{m} {h} * * 6\t{cmd}'
elif day_str == 'каждое воскресенье':
fmt = '{m} {h} * * 0\t{cmd}'
elif every_match:
fmt = '*/{interval} * * * *\t{cmd}'.format(interval=every_match.group(1), cmd=cmd)
sys.stderr.write(' in: {0}\n out: {1}\n'.format(day_str, fmt))
elif day_str == 'будний день':
fmt = '{m} {h} * * 1-5\t{cmd}'
else:
sys.stderr.write('unknown day syntax: {0}\n'.format(day_str))
sys.exit(1)
return fmt.format(h=hour, m=minute, cmd=cmd)
def main():
for line in sys.stdin:
line = line.rstrip()
if not line or line.startswith('\t'):
continue
try:
cron_line = convert(line)
except Exception:
sys.stderr.write('Input: "{0}"\n'.format(line))
raise
sys.stdout.write(cron_line + '\n')
if __name__ == '__main__':
main()
#!/bin/bash -ex
url='https://docs.google.com/spreadsheet/pub?key=...&single=true&gid=0&output=txt'
curl -fsS "$url" >tmp-cron-table
./table2cron <tmp-cron-table >tmp-cron-new
sudo -i crontab -l >tmp-cron-current
diff -u tmp-cron-current tmp-cron-new || true
read -p 'Enter - OK, Ctrl+C - cancel'
sudo -i crontab $PWD/tmp-cron-new
rm -f tmp-cron-{table,current,new}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment