Since you want to be able to control via command line, one way to do this is to use a temporary "flag" file, that will be created by myprogram.py start, and deleted by myprogram.py stop. The key is that, while the file exists, myprogram.py will keep running the loop.
import os
import sys
import time
FLAGFILENAME = 'startstop.file'
def set_file_flag(startorstop):
# In this case I am using a simple file, but the flag could be
# anything else: an entry in a database, a specific time...
if startorstop:
with open(FLAGFILENAME, "w") as f:
f.write('run')
else:
if os.path.isfile(FLAGFILENAME):
os.unlink(FLAGFILENAME)
def is_flag_set():
return os.path.isfile(FLAGFILENAME)
def get_from_ftp(server, login, password):
print("Still running...")
time.sleep(1)
def main():
if len(sys.argv) < 2:
print "Usage: <program> start|stop"
sys.exit()
start_stop = sys.argv[1]
if start_stop == 'start':
print "Starting"
set_file_flag(True)
if start_stop == 'stop':
print "Stopping"
set_file_flag(False)
server, login, password = 'a', 'b', 'c'
while is_flag_set():
get_from_ftp(server, login, password)
print "Stopped"
if __name__ == '__main__':
main()
As you can imagine, the flag could be anything else. This is very simple, and if you want to have more than two instances running, then you should at least name the files differently per instance (for example, with a CLI parameter) so you can selectively stop each instance.
I do like the idea proposed by @cdarke about intercepting and handling CTRL+C, though, and the mechanism is very similar to my approach, and works well with a single instance.
ps. I don't understand why you don't wish to usekill.