나는 라디오 관리자 서비스인 첫 번째 시스템 서비스를 작성했습니다. 일회용이며 시스템 시작 및 종료 시 실행되도록 설계되었습니다. Wi-Fi 및/또는 Bluetooth를 차단하거나 차단 해제하기 위해 rfkill을 호출하므로 NetworkManager에 따라 다릅니다. 꼭 시스템 서비스로 만들지 않는 이상 사용자 서비스로 유지하려고 합니다.
또한 사용자 pi가 loginctl과 일치하도록 시스템을 설정했습니다.
내 문제는 종료 시 서비스가 호출되지만 Wi-Fi 및 블루투스 라디오가 예상대로 차단되지 않는다는 것입니다. 어떤 제안이라도 감사하겠습니다. 감사해요! ! ! ! !
이것은 내 서비스 파일입니다.
[Unit]
Description=RAL Radio Manager BT and Wifi Service
After=network.target
[Service]
Type=oneshot
WorkingDirectory=/home/pi/RAL/radio-manager
Environment=PYTHONUNBUFFERED=1
ExecStart=/usr/bin/python3 /home/pi/RAL/radio-manager/radio_manager.py -bs=1
ExecStop=/usr/bin/python3 /home/pi/RAL/radio-manager/radio_manager.py -bs=0
RemainAfterExit=yes
Restart=no
[Install]
#WantedBy=multi-user.target
WantedBy=default.target
이것은 내 파이썬 스크립트입니다
def radio_manager(args):
MAGNET_OR_SWITCH_GPIO = 4
PROGRAM_EXIT_GPIO = 22
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM ) # .BCM refs the GPIO by the GPIO#, .BOARD by Pin number
GPIO.setup( MAGNET_OR_SWITCH_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_UP) # RPi Header Pin 7, GPIO4, reads the onboard swx state S2.
GPIO.setup(PROGRAM_EXIT_GPIO, GPIO.IN, pull_up_down=GPIO.PUD_DOWN )
#radio_list = rfkill.rfkill_list()
if (args.boot_state == 0):
#Power Down
rfkill.rfkill_blockby('wlan')
print ("Blocking WIFI!!!!!")
rfkill.rfkill_blockby('bluetooth')
print ("Blocking Bluetooth!!!!!")
elif(args.boot_state == 1):
#Power Up
program_exit_pin = GPIO.input(PROGRAM_EXIT_GPIO)
user_requests_wifi = os.path.exists(WIFI_ON_FILE_PATH)
if (program_exit_pin or user_requests_wifi):
rfkill.rfkill_unblockby('wlan')
print ("UnBlocking WIFI!!!!!")
magnet_pin = GPIO.input(MAGNET_OR_SWITCH_GPIO)
if(magnet_pin == 0):
rfkill.rfkill_unblockby('bluetooth')
print ("UnBlocking Bluetooth!!!!!")
return
def main(argv):
args=cli(argv)
radio_manager(args)
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))
편집: 먼저 서비스를 다시 활성화하고 시작했습니다. 22:17:42 이후의 타임스탬프를 확인하세요.
pi@raspberrypi:~$ systemctl --user enable radio_manager.service
Created symlink /home/pi/.config/systemd/user/default.target.wants/radio_manager.service â /home/pi/.config/systemd/user/radio_manager.service.
pi@raspberrypi:~$ systemctl --user start radio_manager.service
pi@raspberrypi:~$ journalctl --user-unit radio_manager.service
-- Logs begin at Thu 2020-08-20 20:04:47 BST, end at Thu 2020-08-20 22:17:43 BST
Aug 20 20:05:14 raspberrypi systemd[370]: Starting RAL Radio Manager BT and Wifi
Aug 20 20:05:16 raspberrypi systemd[370]: Started RAL Radio Manager BT and Wifi
Aug 20 21:42:49 raspberrypi systemd[370]: Stopping RAL Radio Manager BT and Wifi
Aug 20 21:42:51 raspberrypi python3[650]: Blocking WIFI!!!!!
Aug 20 21:42:51 raspberrypi python3[650]: Blocking Bluetooth!!!!!
Aug 20 21:42:51 raspberrypi systemd[370]: radio_manager.service: Succeeded.
Aug 20 21:42:51 raspberrypi systemd[370]: Stopped RAL Radio Manager BT and Wifi
Aug 20 22:17:42 raspberrypi systemd[370]: Starting RAL Radio Manager BT and Wifi
Aug 20 22:17:43 raspberrypi systemd[370]: Started RAL Radio Manager BT and Wifi
rfkill은 두 인터페이스가 모두 예상대로 차단 해제되었음을 보여줍니다. sudo 종료 및 시작 후
pi@raspberrypi:~$ journalctl --user-unit radio_manager
-- Logs begin at Thu 2020-08-20 22:22:23 BST, end at Thu 2020-08-20 22:25:04 BST
Aug 20 22:22:51 raspberrypi systemd[359]: Starting RAL Radio Manager BT and Wifi
Aug 20 22:22:53 raspberrypi systemd[359]: Started RAL Radio Manager BT and Wifi
lines 1-3/3 (END)
pi@raspberrypi:~$
pi@raspberrypi:~$ rfkill list
0: phy0: Wireless LAN
Soft blocked: no
Hard blocked: no
1: hci0: Bluetooth
Soft blocked: no
Hard blocked: no
pi@raspberrypi:~$
편집: 영구 시스템 로그를 켰는데 이제 종료 시 내 서비스가 호출되는 것을 볼 수 있지만 아마도 rfkill이 종료되거나 명령 수신을 중지한 이후일 것입니다. 핵심 사항과 함께 종료 시 잘린 시스템 로그를 첨부했습니다.
Aug 21 16:18:23 raspberrypi sshd[522]: pam_unix(sshd:session): session opened fo
Aug 21 16:18:23 raspberrypi systemd-logind[253]: New session c1 of user pi.
Aug 21 16:18:23 raspberrypi systemd[1]: Started Session c1 of user pi.
Aug 21 16:18:30 raspberrypi systemd[1]: systemd-hostnamed.service: Succeeded.
Aug 21 16:23:20 raspberrypi sudo[560]: pi : TTY=pts/0 ; PWD=/home/pi ; USE <----------- Sudo shutdown now issued
Aug 21 16:23:20 raspberrypi sudo[560]: pam_unix(sudo:session): session opened fo
Aug 21 16:23:21 raspberrypi sudo[560]: pam_unix(sudo:session): session closed fo
Aug 21 16:23:21 raspberrypi systemd[1]: Unmounting RPC Pipe File System...
Aug 21 16:23:21 raspberrypi sshd[522]: pam_unix(sshd:session): session closed fo
Aug 21 16:23:21 raspberrypi sshd[522]: pam_systemd(sshd:session): Failed to rele
Aug 21 16:23:21 raspberrypi systemd[1]: Stopping Session c1 of user pi.
Aug 21 16:23:21 raspberrypi systemd[1]: systemd-rfkill.socket: Succeeded. <------------ Is this the service I need to set the radio status using rfkill
Aug 21 16:23:21 raspberrypi systemd[1]: Closed Load/Save RF Kill Switch Status /
Aug 21 16:23:21 raspberrypi systemd[1]: Stopped target Bluetooth.
Aug 21 16:23:21 raspberrypi systemd[364]: run-rpc_pipefs.mount: Succeeded.
Aug 21 16:23:21 raspberrypi systemd[1]: Stopping Authorization Manager...
Aug 21 16:23:23 raspberrypi systemd[1]: Stopping Manage Sound Card State (restor
Aug 21 16:23:23 raspberrypi systemd[364]: Stopping RAL Radio Manager BT and Wifi <------ SystemD executing ExecStop for my service
Aug 21 16:23:23 raspberrypi systemd[1]: Removed slice system-getty.slice.
Aug 21 16:23:23 raspberrypi systemd[1]: rc-local.service: Succeeded.
Aug 21 16:23:23 raspberrypi systemd[1]: Stopped /etc/rc.local Compatibility.
Aug 21 16:23:23 raspberrypi alsactl[290]: alsactl daemon stopped
Aug 21 16:23:23 raspberrypi systemd[1]: systemd-logind.service: Succeeded.
Aug 21 16:23:23 raspberrypi systemd[1]: Stopped Login Service.
Aug 21 16:23:23 raspberrypi systemd[1]: raspi-config.service: Succeeded.
Aug 21 16:23:23 raspberrypi systemd[1]: Stopped LSB: Switch to ondemand cpu gove
Aug 21 16:23:23 raspberrypi systemd[1]: alsa-state.service: Succeeded.
Aug 21 16:23:23 raspberrypi systemd[1]: Stopped Manage Sound Card State (restore
Aug 21 16:23:24 raspberrypi systemd[1]: hciuart.service: Succeeded.
Aug 21 16:23:24 raspberrypi systemd[1]: Stopped Configure Bluetooth Modems conne
Aug 21 16:23:24 raspberrypi wpa_supplicant[282]: wlan0: CTRL-EVENT-DISCONNECTED
Aug 21 16:23:24 raspberrypi wpa_supplicant[282]: dbus: wpa_dbus_property_changed
Aug 21 16:23:24 raspberrypi dhclient[398]: receive_packet failed on wlan0: Netwo
Aug 21 16:23:25 raspberrypi python3[591]: Blocking WIFI!!!!! <------ My service is trying to Softblock WIFI
Aug 21 16:23:25 raspberrypi python3[591]: Blocking Bluetooth!!!!! <------ My service is trying to Softblock Bluetooth
Aug 21 16:23:24 raspberrypi wpa_supplicant[282]: wlan0: CTRL-EVENT-REGDOM-CHANGE
Aug 21 16:23:25 raspberrypi wpa_supplicant[282]: rfkill: WLAN soft blocked
Aug 21 16:23:25 raspberrypi wpa_supplicant[282]: rfkill: WLAN soft blocked
Aug 21 16:23:25 raspberrypi wpa_supplicant[282]: nl80211: Failed to open /proc/s
Aug 21 16:23:25 raspberrypi wpa_supplicant[282]: nl80211: Failed to set IPv4 uni
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info> [1598023405.1102] manag
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info> [1598023405.1197] devic
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info> [1598023405.1415] dhcp4
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info> [1598023405.1441] dhcp4
Aug 21 16:23:25 raspberrypi NetworkManager[269]: <info> [1598023405.2310] dns-m
Aug 21 16:23:25 raspberrypi systemd[364]: radio_manager.service: Succeeded.
Aug 21 16:23:25 raspberrypi systemd[364]: Stopped RAL Radio Manager BT and Wifi <------ Systemd stopped by service
Aug 21 16:23:25 raspberrypi systemd[364]: Stopped target Basic System.
Aug 21 16:23:25 raspberrypi systemd[364]: Stopped target Timers.
Aug 21 16:23:25 raspberrypi systemd[364]: Stopped target Sockets.
답변1
이후 = network.target
사용자 단위는 시스템 단위에 종속될 수 없습니다. 여기에는 "순서 종속성"이 포함됩니다. 예를 들어 After=
network.target은 시스템 단위입니다. 따라서 이 줄은 아무 작업도 수행하지 않습니다.
반면에 사용자 단위는 시스템 단위 내부에서 실행됩니다 [email protected]
. 해당 장치는 After=network.target 이후에 (간접적으로) 주문되었습니다.
꽉 막힌 상태에서는 polkit.service
종료 후 즉시 종료되어 재 활성화가 불가능할 수도 있다고 생각합니다. 어쩌면 이것이 당신의 문제일지도 모릅니다. polkit은 로그인한 사용자가 rfkill을 작동하도록 허용하는 등의 권한을 부여하는 데 사용됩니다.