By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,270 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,270 IT Pros & Developers. It's quick & easy.

appended crontab entries with py script

P: n/a
rbt
How can I safely append a crontab entry to a crontab file
progammatically with Python?

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?
Sep 13 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
rbt <rb*@athop1.ath.vt.edu> 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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 14 '05 #2

P: n/a
rbt
On Tue, 2005-09-13 at 23:18 -0400, Mike Meyer wrote:
rbt <rb*@athop1.ath.vt.edu> 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')
cur_cron.read()
cur_cron.close()
fp = file('current_crontab.txt', 'a')
print >> fp, "0 * * * * %s/.theft_recovery.py" %home
fp.close()
load = os.popen('crontab current_crontab.txt')
load.read()
load.close()

Sep 14 '05 #3

P: n/a
rbt
I have a win32 service written in Python. It works well. It sends a
report of the status of the machine via email periodically. The one
problem I have is this... while trying to send an email, the script
loops until a send happens and then it breaks. Should it be unable to
send, it sleeps for 10 minutes with time.sleep(600) and then wakes and
tries again. This is when the problem occurs. I can't stop the service
while the program is sleeping. When I try, it just hangs until a reboot.
Can some suggest how to fix this?

Thanks,
rbt

Sep 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.