appended crontab entries with py script

rbt rbt at
Wed Sep 14 17:30:59 CEST 2005

On Tue, 2005-09-13 at 23:18 -0400, Mike Meyer wrote:
> rbt <rbt at> writes:
> > How can I safely append a crontab entry to a crontab file
> > progammatically with Python?
> Well, one way would be to invoke the system crontab utility and use an
> "editor" that passes the file to your program, and reads the results
> back.
> > I need to handle crontabs that currently have entries and crontabs that
> > are empty. Also, I'd like this to work across Linux and BSD systems.
> >
> > Any pointers?
> I think most Free Unix systems use the Vixie cron, and the non-free
> ones have a "crontab" command (do some of them call it cron?) with the
> same API. So you're pretty safe using that.
> If you want to assume that you're going to have the vixie cron, you
> could dig into it's guts to see what it does for locking, and do that
> by hand.
>    <mike

Here's what I did... can you write uglier code than this ;)

Works on Mac and Linux... for the most part.

def add_cron_entry():
    home = os.path.expanduser('~')
    cur_cron = os.popen('crontab -l > current_crontab.txt')
    fp = file('current_crontab.txt', 'a')
    print >> fp, "0 * * * * %s/" %home
    load = os.popen('crontab current_crontab.txt')

More information about the Python-list mailing list