Skip to content

Instantly share code, notes, and snippets.

@methane
Created May 4, 2012 10:29
Show Gist options
  • Select an option

  • Save methane/2593937 to your computer and use it in GitHub Desktop.

Select an option

Save methane/2593937 to your computer and use it in GitHub Desktop.
crontab の自動バックアップ
#!/usr/bin/env python
# coding: utf-8
import os
import subprocess
import sys
def escape(x):
return x.replace("'", "\\'").replace('"', '\\"')
def sh(command, *args):
if args:
command = command % tuple(map(escape, args))
return subprocess.call(command, shell=True)
# git init しておく
GIT_PATH = os.path.expanduser('~/crontab')
CRONTAB = '/usr/bin/crontab'
if '-e' in sys.argv:
ret = sh([CRONTAB] + sys.argv[1:])
if ret == 0:
sh("%s -l > '%s'", CRONTAB, GIT_PATH+'/crontab')
os.chdir(GIT_PATH)
sh("git add crontab")
sh("git commit -m 'autocommit'")
else:
os.execl(CRONTAB, CRONTAB, *sys.argv[1:])
# ft=python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment