Last active
August 29, 2015 14:06
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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