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()
아마도이 질문에 도움이 될 수 있습니까? https://stackoverflow.com/questions/18499497/how-to-process-sigterm-signal-gracefully – csim
또는 https://stackoverflow.com/questions/1112343/how-do-i-capture-sigint-in -python – languitar