[Tutor] Running a script in the background
alan.gauld at btinternet.com
Sun Sep 2 08:34:05 CEST 2012
On 02/09/12 04:29, Michael Lewis wrote:
> I have a script that will run forever. Since it runs forever, I don't
> want to see the interpreter or command line. I want the program to run
> in the background so I don't see it at all.
That's an OS thing not a Python thing.
On Unix it means adding an entry in the startup scripts.
For an old BSD type like me that was one of the init scripts in the /etc
tree but nowadays there are different mechanisms, and I'm especially
unclear on how MacOS/Darwin starts up. You'll need to do some Googling.
On windows you can just put it into the Run registry entry under the
appropriate key. Again search the Microsoft help system for the answers.
> How can I do this? For some background, my script essentially check
> every x minutes to see if any files have been updated and then moves
> them to dropbox.
In that case using a long running Python script is probably the wrong
answer. You would be better with a short running, do it once, script
that the OS launches regularly.
Look at 'cron' on *nix and 'at' on Windows.
That will be less resource hungry, there is no point in having running
programs that do nothing for most of the time.
> ... I can create two separate scripts to separate out the two OS's.
One of the beauties of Python is that you rarely need separate scripts.
Put the file location(s) in a config file and the same script will work
for both. Only the startup mechanism will differ.
Author of the Learn to Program web site
More information about the Tutor