DREAMER

[service] Auto-restart a failed service in systemd 본문

프로그래밍/Linux

[service] Auto-restart a failed service in systemd

연소민 2023. 5. 17. 19:36
728x90
반응형

AP: NVIDIA Jetson Nano

Kernel: 4.14

 

연결된 블루투스 장치가 특정이유로 connected, disconnected를 반복하다보면 Agent registered라고 뜨며, bluetoothd이 죽는 경우가 발생한다. (Segmentation fault, SIGSEGV(11) 발생)

이 때 bluetooth.service의 status는 Failed이다.

단순히 bluetooth.service를 restart하면 되지만, 다음과 같이 service 설정을 변경하면, failed시 자동으로 restart를 한다.

 

보통 아래와 같이 bluetooth.service가 생성되어있을 것이다.

$ cat /lib/systemd/system/bluetooth.service
[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth

[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd
NotifyAccess=main
#WatchdogSec=10
#Restart=on-failure
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full

[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service

Restart와 RestartSec를 설정해준다. RestartSec의 기본 값은 5s이다.

[Unit]
Description=Bluetooth service
Documentation=man:bluetoothd(8)
ConditionPathIsDirectory=/sys/class/bluetooth

[Service]
Type=dbus
BusName=org.bluez
ExecStart=/usr/lib/bluetooth/bluetoothd
NotifyAccess=main
#WatchdogSec=10
Restart=on-failure	# comment for auto-restart when failed
RestartSec=2s		# set restart time to 2sec
CapabilityBoundingSet=CAP_NET_ADMIN CAP_NET_BIND_SERVICE
LimitNPROC=1
ProtectHome=true
ProtectSystem=full

[Install]
WantedBy=bluetooth.target
Alias=dbus-org.bluez.service

장치 재부팅 후 bluetooth.service에 대한 설정 값들을 확인할 수 있다.

$ systemctl show bluetooth
$ systemctl show bluetooth | grep Restart
Restart=on-failure
RestartUSec=2s
NRestarts=0

실제로 restart하는지 확인하고 싶다면, bluetoothd가 똑같은 예외를 발생하여 종료하게 하면 된다.

$ ps -ef | grep bluetoothd
$ kill -11 [bluetoothd pid]
예) 
  $ ps -ef | grep bluetoothd
  4800 bluetoothd
  $ kill -11 4800

설정한 시간에 맞춰 restart를 하는 것을 알 수 있다.

728x90
반응형
Comments