How can I turn my python script off from the terminal?
Question: I have just made a script that I want to turn off from the terminal, but
instead of just ending it I want it to pickle a file. Is there a correct way
to do this?


Answer: Have a look at how [signals](https://docs.python.org/2/library/signal.html)
works.

You can basically make your script wait for a signal on QUIT:

    
    
    #!/usr/bin/python
    
    from signal import *
    from time import sleep
    
    def process_quit(signum, frame):
        # Do your pickle thing here
        print('Exiting...')
    
    
    signal(SIGQUIT, process_quit)
    
    while 1:
        print('Working hard')
        sleep(0.5)
    

You can also register an handler with `atexit`:

    
    
    import atexit
    
    def exit_handler():
        print 'My application is ending!'
    
    atexit.register(exit_handler)
    

