0

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

3
  • A better solution is probably to create a system-level service for your program. What operating system are you using? Commented Oct 5, 2018 at 12:14
  • 3
    You can raise signals from a python program, and they don't have to be SIGKILL but something like SIGINT (Ctrl+C) which could be trapped for a tidy closedown. The PID could be written to a file by the original to avoid the use of ps. I don't understand why you don't wish to use kill. Commented Oct 5, 2018 at 12:22
  • I don't want use kill because I am afraid that if during big file downloading, download will interrupt. But if I will stop cycling file will be downloaded and then script will be closed. Commented Oct 5, 2018 at 15:21

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0
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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.