I'm using Python 2.6 on linux.
I have a run.py script which starts up multiple services in the background and generates kill.py to kill those processes.
Inside kill.py, is it safe to unlink itself when it's done its job?
# kill services
# is it safe to do something here?
time.sleep(10) # sleep 1
time.sleep(10) # sleep 2
Don't have your scripts write new scripts if you can avoid it – just write out a list of the PIDs, and then through them. It's not very clear what you're trying to do, but creating and deleting scripts sounds like too much fragile magic.
To answer the question: Python compiles all of the source and closes the file before executing it, so this is safe.
In general, unlinking an opened file is safe on Linux. (But not everywhere: on Windows you can't delete a file that is in use.)
Note that when you
import a module, Python 2 compiles it into a
.pyc bytecode file and interprets that. If you remove the
.py file, Python will still use the
.pyc, and vice versa.