Bash
systemd 通過在腳本名稱前添加 0000:00:14.0 來破壞 $0
我有一個腳本
/lib/systemd/system-sleep
可以從命令行完美執行:$ ll /lib/systemd/system-sleep/smartplug -rwxr-xr-x 1 root root 713 Feb 7 04:55 /lib/systemd/system-sleep/smartplug* $ sudo /lib/systemd/system-sleep/smartplug pre Suspend /lib/systemd/system-sleep/smartplug: Going to Suspend... /lib/systemd/system-sleep/smartplug: Status of: 192.168.0.15 is: OFF $ sudo /lib/systemd/system-sleep/smartplug post Suspend /lib/systemd/system-sleep/smartplug: Waking up from Suspend...
但是,當
systemd
在掛起期間呼叫腳本時,它會“污染”腳本的名稱並從被呼叫的腳本返回 null:$ journalctl -b-0 | grep smartplug Feb 07 05:47:30 alien systemd-sleep[32243]: /lib/systemd/system-sleep/smartplug: Going to suspend... Feb 07 05:47:30 alien systemd-sleep[32243]: 0000:00:14.0/lib/systemd/system-sleep/smartplug: Status of: 192.168.0.15 is: Feb 07 16:36:10 alien systemd-sleep[32243]: /lib/systemd/system-sleep/smartplug: Waking up from suspend...
systemd
應該說:Feb 07 05:47:30 alien systemd-sleep[32243]: /lib/systemd/system-sleep/smartplug: Status of: 192.168.0.15 is: OFF
但它在說:
Feb 07 05:47:30 alien systemd-sleep[32243]: 0000:00:14.0/lib/systemd/system-sleep/smartplug: Status of: 192.168.0.15 is:
我以前從未見過這種奇怪的行為。以下是腳本內容:
#!/bin/bash # NAME: smartplug # PATH: /lib/systemd/system-sleep # CALL: Called from SystemD automatically # DESC: When suspending turn off Kasa smartplugs. # NOTE: Copy hs100.sh and myip.sh from user directory to /usr/bin. # DATE: Feb 6 2020. TMPLIST=/tmp/smartplug-list PlugArr=( "192.168.0.15" ) case $1/$2 in pre/*) echo "$0: Going to $2..." echo -n '' > "$TMPLIST" status=$(hs100.sh -i "${PlugArr[0]}" check | cut -f2) echo "$0: Status of: ${PlugArr[0]} is: $status" if [[ "$status" == ON ]] ; then hs100.sh -i "${PlugArr[0]}" off echo "${PlugArr[0]}" >> "$TMPLIST" fi ;; post/*) echo "$0: Waking up from $2..." rm $TMPLIST ;; esac
回複評論
結果來自
lspci -vt
$ lspci -vt -[0000:00]-+-00.0 Intel Corporation Xeon E3-1200 v5/E3-1500 v5/6th Gen Core Processor Host Bridge/DRAM Registers +-01.0-[01]--+-00.0 NVIDIA Corporation GM204M [GeForce GTX 970M] | \-00.1 NVIDIA Corporation GM204 High Definition Audio Controller +-02.0 Intel Corporation HD Graphics 530 +-04.0 Intel Corporation Xeon E3-1200 v5/E3-1500 v5/6th Gen Core Processor Thermal Subsystem +-14.0 Intel Corporation 100 Series/C230 Series Chipset Family USB 3.0 xHCI Controller
結果來自
journalctl -b-0 | grep smartplug -A5
這確認斷開 PCI 匯流排是問題消息的根源
-- Feb 07 22:40:15 alien systemd-sleep[14761]: /lib/systemd/system-sleep/smartplug: Going to suspend... Feb 07 22:40:15 alien systemd-sleep[14761]: /lib/systemd/system-sleep/custom-xhci_hcd: Going to suspend... Feb 07 22:40:15 alien systemd-sleep[14761]: Failed to connect to non-global ctrl_ifname: (nil) error: No such file or directory Feb 07 22:40:15 alien systemd-sleep[14762]: /lib/systemd/system-sleep/wpasupplicant failed with error code 255. Feb 07 22:40:15 alien systemd-sleep[14762]: /lib/systemd/system-sleep/r8169-reset failed with error code 1. Feb 07 22:40:15 alien kernel: xhci_hcd 0000:00:14.0: remove, state 4 -- Feb 07 22:40:15 alien systemd-sleep[14761]: 0000:00:14.0/lib/systemd/system-sleep/smartplug: Status of: 192.168.0.15 is: Feb 07 22:40:15 alien kernel: usb 1-9: USB disconnect, device number 9 Feb 07 22:40:15 alien acpid[1320]: input device has been disconnected, fd 22 Feb 07 22:40:15 alien kernel: xhci_hcd 0000:00:14.0: USB bus 1 deregistered Feb 07 22:40:15 alien kernel: xhci_hcd 0000:39:00.0: remove, state 4 Feb 07 22:40:15 alien kernel: usb usb4: USB disconnect, device number 1 --
請注意,他們的行
xhci_hcd 0000:00:14.0: USB bus 1 deregistered
是一個腳本custom-xhci_hcd
,其工作是斷開所有 USB 設備(解除綁定/關閉電源)。它導致smartplug
腳本失敗,因為它需要 WiFi 才能關閉 Kasa Smartplug。我還沒有走出困境,因為當筆記型電腦掛起時,Smartplug 仍然沒有關閉電視燈。我認為這是由於網路管理器過快地關閉 WiFi,但需要進行更多調查。
禁用後
custom-xhci_hcd
(幾年前寫的,但核心可能不再需要4.14.170
這是journalctl
:-- Feb 08 09:14:44 alien systemd-sleep[3032]: /lib/systemd/system-sleep/smartplug: Going to suspend... Feb 08 09:14:44 alien systemd-sleep[3032]: Failed to connect to non-global ctrl_ifname: (nil) error: No such file or directory Feb 08 09:14:44 alien systemd-sleep[3033]: /lib/systemd/system-sleep/wpasupplicant failed with error code 255. Feb 08 09:14:44 alien eyesome[3127]: Wakeup: Creating /tmp/eyesome-is-suspending Feb 08 09:14:44 alien systemd-sleep[3032]: /lib/systemd/system-sleep/smartplug: Status of: 192.168.0.15 is: Feb 08 09:14:44 alien systemd-sleep[3033]: /lib/systemd/system-sleep/display-auto-brightness failed with error code 1. Feb 08 09:14:45 alien systemd-sleep[3033]: /lib/systemd/system-sleep/lag-suspend.sh failed with error code 1. Feb 08 09:14:45 alien systemd-sleep[3033]: /lib/systemd/system-sleep/r8169-reset failed with error code 1. Feb 08 09:14:45 alien systemd-sleep[3033]: /lib/systemd/system-sleep/iwlwifi-reset failed with error code 1. Feb 08 09:14:45 alien systemd-sleep[3032]: Suspending system... -- Feb 08 09:14:57 alien systemd-sleep[3032]: /lib/systemd/system-sleep/smartplug: Waking up from suspend... Feb 08 09:14:57 alien systemd-sleep[3032]: Failed to connect to non-global ctrl_ifname: (nil) error: No such file or directory Feb 08 09:14:57 alien kernel: PM: suspend exit Feb 08 09:14:57 alien eyesome[3346]: Wakeup: Called from suspend. Feb 08 09:14:57 alien systemd-sleep[3032]: /dev/sda: Feb 08 09:14:57 alien systemd-sleep[3032]: setting Advanced Power Management level to 0xfe (254)
腳本中的其他錯誤程式碼
/lib/systemd/system-sleep
是因為腳本不可執行,但由於歷史原因而保留。rick@alien:/lib/systemd/system-sleep$ ll total 68 drwxr-xr-x 2 root root 4096 Feb 7 04:55 ./ drwxr-xr-x 8 root root 4096 Feb 6 16:53 ../ -rw-r--r-- 1 root root 1079 Oct 28 2018 custom-xhci_hcd -rw-r--r-- 1 root root 1079 Oct 28 2018 custom-xhci_hcd~ -rw-r--r-- 1 root root 1539 Jun 10 2018 display-auto-brightness -rwxr-xr-x 1 root root 92 Mar 17 2016 hdparm* -rw-r--r-- 1 root root 716 Apr 22 2017 iwlwifi-reset -rw-r--r-- 1 root root 572 Oct 28 2018 lag-suspend.sh -rw-r--r-- 1 root root 522 Oct 21 2018 lag-suspend.sh~ -rw-r--r-- 1 root root 2820 Aug 5 2018 r8169-reset -rwxr-xr-x 1 root root 713 Feb 7 04:55 smartplug* -rwxr-xr-x 1 root root 661 Feb 7 04:53 smartplug~* -rwxr-xr-x 1 root root 1114 Oct 28 2018 sound* -rwxr-xr-x 1 root root 1171 Aug 5 2018 sound~* -rwxr-xr-x 1 root root 317 Aug 29 05:44 systemd-wake-eyesome* -rwxr-xr-x 1 root root 219 Apr 29 2019 unattended-upgrades* -rwxr-xr-x 1 root root 182 Oct 26 2015 wpasupplicant*
結果來自
cat -v
根據評論請求:
$ sudo /lib/systemd/system-sleep/smartplug pre Suspend |& cat -v /lib/systemd/system-sleep/smartplug: Going to Suspend... /lib/systemd/system-sleep/smartplug: Status of: 192.168.0.15 is: ON
注意管道
|& cat -v
不會改變輸出。
Systemd 首先關閉網路管理器
即使通過在掛起期間不斷開 USB 匯流排來更正錯誤消息後,問題仍然存在,您無法在掛起期間發送 WiFi 命令。
網路管理器是在掛起/休眠和關機期間關閉的第一個服務。如果您想在這些時間向設備發送 WiFi 無線電信號,您需要閱讀此內容。
將您的腳本移出 Systemd “hacks”
為了解決我的問題,我創建了腳本:
/etc/NetworkManager/dispatcher.d/pre-down.d/smartplug_off
該腳本必須標記為可執行(
chmod a+x scriptname
),對我來說它包含:#!/bin/bash # NAME: smartplug_off # PATH: /etc/NetworkManager/dispatcher.d/pre-down.d # DESC: Turn off smartplug light power for TV light # DATE: March 7, 2020. # CALL: Called by Network Manager before going down. Network manager in turn # is called by systemd during suspend/hibernate/shutdown # NOTE: myisp.sh and hs100.sh must be installed for hs100 tp-link power plug. # https://developer.gnome.org/NetworkManager/stable/NetworkManager.html PlugName="192.168.0.15" status=$(hs100.sh -i "$PlugName" check | cut -f2) if [ $status == "OFF" ] ; then : # Nothing to do already off elif [ $status == "ON" ] ; then hs100.sh -i "$PlugName" off else echo Error hs100.sh not responding check connection and IP "$PlugName". fi