Is there a way to schedule my script?

Michael Torrie torriem at gmail.com
Thu Dec 18 11:45:15 EST 2014


On 12/17/2014 01:42 PM, Juan Christian wrote:
> On Wed Dec 17 2014 at 6:25:39 PM John Gordon <gordon at panix.com> wrote:
> If you want to solve your problem entirely within Python, look at the
> "scheduler" module. (Although even this isn't a complete solution, as you
> still have to make sure the program is running in the first place...)
> 
> 
> My script is running fine, Win/OSX/Linux and I don't want to ruin that
> using system specific things.

Wrong.  You don't have to change or ruin your script.  If your script is
done right, you put all the real work inside of callable functions
anyway, and probably have some sort of "main" function that gets called
in a manner similar to this:

if __name__=="__main__":
    my_main()

If so, then you create platform-dependent code in another file that
simply imports your existing, working script as a module and runs that
main function.  On Windows you write a service API wrapper.  On Linux
you can run the script directly from cron.  On Mac you can just bundle a
launchd control file (or use cron).

If your script is not coded in such a fashion as to make turning it into
an importable module easy, I highly recommend changing your to work in
this way.  Once my python programs get halfway useful I always try to
reorganize my code in such a way that it can be used as a module.
Because invariable I find that I do want to add another layer, and the
modules are ideal for this.  Nearly every script has the "if
__name__=='__main__'" block at the end of the file, where it provides
standalone features such as a command-line interface, or provides
testing of the module's features.




More information about the Python-list mailing list