반응형
# https://stackoverflow.com/questions/48506824/automatic-start-and-stop-of-apscheduler-during-boot-of-linux
import signal
import atexit
import os
import time
PID_FILE_PATH = "pythonPID.pid"
stop = False
def create_pid_file():
# this creates a file called program.pid
with open(PID_FILE_PATH, "w") as fhandler:
# and stores the PID in it
fhandler.write(str(os.getpid()))
def sigint_handler(signum, frame):
print("Cleanly exiting")
global stop
# this will break the main loop
stop = True
def exit_handler():
# delete PID file
os.unlink(PID_FILE_PATH)
def main():
create_pid_file()
# this makes exit_handler to be called automatically when the program exists
atexit.register(exit_handler)
# this makes sigint_handler to be called when a signal SIGTERM
# is sent to the process, e.g with command: kill -SIGTERM $PID
signal.signal(signal.SIGTERM, sigint_handler)
while not stop:
# this represents your main loop
pass
#time.sleep(10)
if __name__ == "__main__":
main()
'Linux > Python' 카테고리의 다른 글
simple python daemon (0) | 2021.11.01 |
---|---|
Python argument vscode debug (0) | 2021.10.30 |
Python socket(Ping3, Ping) 관련 Permisson Error 해결 (0) | 2021.10.26 |
Python을 이용한 RAID상태 모니터링 (0) | 2019.04.15 |