2017-04-30 4 views
1

서비스로 실행되는 Python 스크립트가 있습니다. 디스크에 씁니다. 사용자가 서비스에서 systemctl stop을 호출하면 파일 손상 위험을 줄이기 위해이 명령을 직접 처리하고 싶습니다.파이썬이 systemctl을 어떻게 처리 할 수 ​​있습니까?

어떻게 systemctl stop 명령을 사용할 수 있습니까? 는/usr/lib 디렉토리/systemd/시스템

내 파일은 다음과 같습니다

[Unit] 
Description=foo 

[Service] 
Type=simple 
ExecStart=/usr/bin/python /srv/go.py 
User=jon 
Restart=always 

[Install] 
WantedBy=graphical.target 

내 파이썬 스크립트입니다 : 다음, 즉시 systemd documentation에서 설명한 바와 같이

#!/usr/bin/python 

import time 
import datetime 
import threading 

def worker(): 
    while True: 

     with open('/tmp/go.txt', 'a') as f: 
      s = str(datetime.datetime.utcnow()) + '\n' 
      f.write(s) 
      print(s) 

      time.sleep(1) 


if __name__=='__main__': 
    t = threading.Thread(target=worker) 
    t.start() 
+0

아마도이 질문에 도움이 될 수 있습니까? https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully – csim

+0

또는 https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in -python – languitar

답변

4

는 프로세스 SIGTERM을 받게되며, SIGHUP. 얼마 후 SIGKILL이 전송됩니다.

모든 신호와 신호 전송 전략은 관련 서비스 파일에서 변경할 수 있습니다.

another stackoverflow question을 참조하여 파이썬 프로그램에서 이러한 신호를 처리하는 방법을 확인하십시오.