Systemd:Pythonサービスを停止した後に再起動できません。

Systemd:Pythonサービスを停止した後に再起動できません。

Raspbian Jessieはsystemdによって自動的に実行されるPythonスクリプトを書いています。 SSCCEとして、単純なPythonスクリプトをサービスとして書いてみましょう。

cat >/home/pi/service.py <<EOF
from time import sleep
sleep(99999)
EOF

これはSIGINTを介して中断される可能性があるため、次のようにサービスを設定し、SIGINTがサービスを終了する正しい方法であるとsystemdに通知しました。

cat >/lib/systemd/system/python-service.service <<EOF
[Unit]
Description=Python service

[Service]
Type=simple
TTYPath=/dev/tty1
StandardInput=tty
StandardOutput=tty
ExecStart=/usr/bin/nohup /usr/bin/python /home/pi/service.py
KillSignal=SIGINT
SuccessExitStatus=SIGINT

[Install]
WantedBy=multi-user.target

EOF

その後、サービスを開始するようにsystemdを設定しました。

systemctl set-default multi-user.target
systemctl daemon-reload
systemctl enable python-service.service

サービスが正しく設定され、実行中で停止できますが、再起動しないと再起動できません。

# I first check that the service has been run at startup:
pi@raspberrypi:~ $ systemctl status python-service.service 
● python-service.service - Python service
   Loaded: loaded (/lib/systemd/system/python-service.service; enabled)
   Active: active (running) since Mon 2017-01-09 01:56:47 UTC; 59s ago
 Main PID: 419 (python)
   CGroup: /system.slice/python-service.service
           └─419 /usr/bin/python /home/pi/service.py

# It runs fine, now I stop it
pi@raspberrypi:~ $ sudo systemctl stop python-service.service 

# Let's start it again
pi@raspberrypi:~ $ sudo systemctl start python-service.service 

# And consult the current status, I assume the parenthesis mean there is an issue
pi@raspberrypi:~ $ systemctl status python-service.service 
● python-service.service - Python service
   Loaded: loaded (/lib/systemd/system/python-service.service; enabled)
   Active: active (running) since Mon 2017-01-09 01:59:54 UTC; 2s ago
 Main PID: 833 ((nohup))
   CGroup: /system.slice/python-service.service
           └─833 (nohup)

# And indeed no service.py is running
pi@raspberrypi:~ $ ps ax|grep service
  839 pts/0    S+     0:00 grep --color=auto service

ベストアンサー1

おすすめ記事