
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