Pavel Mihaylyan
Pavel Mihaylyan

Reputation: 9

How to stop python script using command line

I have python script which download/upload files to/from FTP server in background.

run = 1
while run == 1:    
   get_from_ftp(server, login, password)

I want to run and stop my python script using command line

Like:

myprogram.py start and myprogram.py stop

The idea is folowing when i run command myprogram.py stop variable run should get value 0 and cycle (while) should upload/download last file and stop.

Please suggest how can i realize it.

Please don't suggest use kill, ps and ctrl+c

Upvotes: 0

Views: 4988

Answers (3)

sal
sal

Reputation: 3593

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.

Upvotes: 1

kishs1991
kishs1991

Reputation: 1059

You can use below -

import sys
import os

text_file = open('Output.txt', 'w')
text_file.write(sys.argv[1])
text_file.close()

if sys.argv[1] == "stop":
    sys.exit(0)

run = 1
while run == 1:

#   Your code   

    fw = open('Output.txt', 'r')
    linesw = fw.read().replace('\n', '')
    fw.close()
    if linesw == "stop":
        os.remove('Output.txt')
        break
sys.exit(0)

I am using Python 3.7.0. Run it like python myprogram.py start and python myprogram.py stop.

Upvotes: 0

C.med
C.med

Reputation: 601

while True: 
   run= int(input("press 1 to run/ 0 to stop: "))
   if run == 1:   
        get_from_ftp(server, login, password)
   elif run ==0:
        # what you want to do

If I understand your question is could be something like that

Upvotes: 0

Related Questions