Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a Cron like scheduler in Python? [closed]

I'm looking for a library in Python which will provide at and cron like functionality.

I'd quite like have a pure Python solution, rather than relying on tools installed on the box; this way I run on machines with no cron.

For those unfamiliar with cron: you can schedule tasks based upon an expression like:

 0 2 * * 7 /usr/bin/run-backup # run the backups at 0200 on Every Sunday  0 9-17/2 * * 1-5 /usr/bin/purge-temps # run the purge temps command, every 2 hours between 9am and 5pm on Mondays to Fridays. 

The cron time expression syntax is less important, but I would like to have something with this sort of flexibility.

If there isn't something that does this for me out-the-box, any suggestions for the building blocks to make something like this would be gratefully received.

Edit I'm not interested in launching processes, just "jobs" also written in Python - python functions. By necessity I think this would be a different thread, but not in a different process.

To this end, I'm looking for the expressivity of the cron time expression, but in Python.

Cron has been around for years, but I'm trying to be as portable as possible. I cannot rely on its presence.

like image 672
jamesh Avatar asked Dec 17 '08 00:12

jamesh


People also ask

How do you end a schedule in Python?

The cancel() method of the scheduler class from the Python Standard Library module sched, removes an already scheduled event from a scheduler instance. The cancel() method does not remove an event from the queue once the scheduler is started by calling the run() method.

How do I know if a cron job is running?

To check to see if the cron daemon is running, search the running processes with the ps command. The cron daemon's command will show up in the output as crond. The entry in this output for grep crond can be ignored but the other entry for crond can be seen running as root. This shows that the cron daemon is running.

How do I run a Python script in crontab?

and use the full path of your foo.py file in your crontab . See documentation of execve(2) which is handling the shebang. Show activity on this post. Change to the working directory and execute the script from there, and then you can view the files created in place.


1 Answers

If you're looking for something lightweight checkout schedule:

import schedule import time  def job():     print("I'm working...")  schedule.every(10).minutes.do(job) schedule.every().hour.do(job) schedule.every().day.at("10:30").do(job)  while 1:     schedule.run_pending()     time.sleep(1) 

Disclosure: I'm the author of that library.

like image 139
dbader Avatar answered Oct 07 '22 21:10

dbader