/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。
我还没有摆脱困境,因为当笔记本电脑挂起时,智能插头仍然没有关闭电视灯。我认为这是由于网络管理器过快关闭 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
不会改变输出。
答案1
Systemd 首先关闭网络管理器
即使通过在挂起期间不断开 USB 总线来更正错误消息,问题仍然存在,您无法在挂起期间发送 WiFi 命令。
网络管理器是在挂起/休眠和关闭期间关闭的第一个服务。如果您想在这些时间向设备发送 WiFi 无线电信号,您需要读这个。
将您的脚本移出 Systemd“黑客”
为了解决我的问题,我创建了脚本:
/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