我发现这篇博客文章在这里这解释了使用 systemd 挂起之前和之后运行脚本的过程。步骤如下:
- 创建一个
/usr/lib/systemd/system-sleep/suspend-date.sh
脚本:
#!/bin/sh
if [ "${1}" == "pre" ]; then
# Do the thing you want before suspend here, e.g.:
echo "we are suspending at $(date)..." > /tmp/systemd_suspend_test
elif [ "${1}" == "post" ]; then
# Do the thing you want after resume here, e.g.:
echo "...and we are back from $(date)" >> /tmp/systemd_suspend_test
fi
- 提供执行权限:
chomd +x /usr/lib/systemd/system-sleep/suspend-date.sh
- 暂停电脑
- 唤醒电脑
- 查看发生的事情的日志
nano /tmp/systemd_suspend_test
但,当我进行到第 5 步时,什么也没有发生。该systemd_suspend_test
文件从未创建,因此 - 没有任何内容可查看。
我的故障排除是:
- 确认脚本具有适当的权限:
[/usr/lib/systemd/system-sleep]$ ls -l
-rwxr-xr-x 1 root root 317 Sep 19 14:31 suspend-date.sh
- 在终端中发出命令以确认其正常工作:
echo "we are suspending at $(date)..." > /tmp/systemd_suspend_test && \
echo "...and we are back from $(date)" >> /tmp/systemd_suspend_test
并查看/tmp/systemd_suspend_test
文件:
we are suspending at Wed 20 Sep 2023 02:25:28 PM EDT...
...and we are back from Wed 20 Sep 2023 02:25:28 PM EDT
- 重新启动睡眠系统
$ sudo systemctl restart systemd-sleep
Failed to restart systemd-sleep.service: Unit systemd-sleep.service not found.
当我的计算机进入和退出挂起状态时,如何让我的suspend-date.sh
脚本启动(就像放置在目录中一样)/usr/lib/systemd/system-sleep/
?