Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ############################################################################
- # script.py
- # It has signal's handler so it can close connections/files at the end.
- # It saves its PID in file so admin can use it to send signal to this script
- ############################################################################
- import signal
- import time
- import os
- def exit_gracefully(signum, frame):
- print("closing connections/files ...")
- # closing ... longer time
- time.sleep(3)
- print("end of the program.")
- exit()
- #exit(1) # exit with some code
- #---------------------------------------------------------------------------
- # register signals
- signal.signal(signal.SIGINT, exit_gracefully)
- signal.signal(signal.SIGTERM, exit_gracefully)
- # save PID in file so admin can use it to kill this script
- pid = os.getpid()
- with open('script.lock', 'w') as out:
- out.write(str(pid))
- # long runnig program
- while True:
- print("simulate long running program ...")
- time.sleep(1)
- ############################################################################
- # admin.py
- # It gets script's PID from file and sends signal to close this script
- ############################################################################
- import signal
- import os
- with open('script.lock') as f:
- pid = int(f.read())
- os.kill(pid, signal.SIGTERM)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement