
시스템 단위 파일이 있습니다.
[Unit]
Description = testtesttest
After = network.target
[Service]
Type = simple
ExecStart = /usr/bin/python3.9 -u /home/pi/initialize.py
PIDFile=/home/pi/faulty.pid
User = root
Group = root
Restart = on-failure
RestartSec = 5
TimeoutStartSec = infinity
[Install]
WantedBy = multi-user.target
초기화.py:
import subprocess
# Pretend like there is some initialization here
s = subprocess.run(['su', 'pi', "-c", "python3.9 -u faulty.py"], cwd="/home/pi", text=True)
# faulty.py is located under /home/pi
오류.py:
import os, time
with open("faulty.pid", "w") as f:
f.write(str(os.getpid()))
print("AAAAAAAAAAAAAA")
time.sleep(60)
raise NameError("Boom")
내가 겪고 있는 문제는 다음을 사용하여 failed.py의 출력을 볼 수 없다는 것입니다.systemctl 상태 테스트또는Journalctl -u 테스트. 하지만 나는할 수 있는시스템 로그에서 찾으십시오. 나는 또한 단순 대신 유형 분기를 시도했지만 도움이 되지 않았습니다. 또한 수동으로 초기화.py를 실행하면 결함이 있는.py의 출력이 터미널에 나타납니다.
바꾸다
s = subprocess.run(['su', 'pi', "-c", "python3.9 -u faulty.py"], cwd="/home/pi", text=True)
그리고
s = subprocess.run(["python3.9", "-u", "faulty.py"], cwd="/home/pi", text=True)
또한 예상되는 로그를 생성합니다.
고쳐 쓰다:
s = subprocess.run("sudo -u pi bash -c 'python3.9 -u faulty.py'", cwd='/home/pi', text=True, shell=True)
반품일하다그리고 사용자를 변경합니다. (그래서 sudo가 더 나은 것 같아요)
Journalctl log (case forked):
Aug 03 12:35:42 raspberrypi systemd[1]: Starting testtesttest...
Aug 03 12:35:42 raspberrypi su[52929]: (to pi) root on none
Aug 03 12:35:42 raspberrypi su[52929]: pam_unix(su:session): session opened for user pi(uid=1000) by (uid=0)
Aug 03 12:36:43 raspberrypi systemd[1]: test.service: New main PID 52947 does not exist or is a zombie.
Aug 03 12:36:43 raspberrypi systemd[1]: test.service: Failed with result 'protocol'.
Aug 03 12:36:43 raspberrypi systemd[1]: Failed to start testtesttest.
Aug 03 12:36:48 raspberrypi systemd[1]: test.service: Scheduled restart job, restart counter is at 1.
Aug 03 12:36:48 raspberrypi systemd[1]: Stopped testtesttest.
Journalctl 로그(간단한 사례):
Aug 03 12:42:29 raspberrypi systemd[1]: Started testtesttest.
Aug 03 12:42:29 raspberrypi su[53443]: (to pi) root on none
Aug 03 12:42:29 raspberrypi su[53443]: pam_unix(su:session): session opened for user pi(uid=1000) by (uid=0)
Aug 03 12:43:29 raspberrypi systemd[1]: test.service: Succeeded.