如何在 Debian 9 或 Raspbian 8 (Jessie) 关机时运行脚本

Fra*_*ing 3 shutdown system-v sysvinit

我想在重启和关闭时执行这个 shell 脚本:

#!/bin/sh
touch /test
Run Code Online (Sandbox Code Playgroud)

它的权限是

-rwxr-xr-x 1 root root 22 Feb 24 09:34 /etc/init.d/te1
Run Code Online (Sandbox Code Playgroud)

它有这个链接

/etc/rc0.d/K01te1 -> ../init.d/te1
/etc/rc6.d/K01te1 -> ../init.d/te1
Run Code Online (Sandbox Code Playgroud)

如果我有这个链接,它在启动时工作

/etc/rc5.d/S01te1 -> ../init.d/te1
Run Code Online (Sandbox Code Playgroud)

但我需要它在关机时运行。

我如何在 Debian 8 和 9 测试中做到这一点?

建议 touch /var/lock/subsys/te1没有奏效。

Fra*_*ing 7

我的印象是,其他人似乎在运行这个方面也有问题。似乎从 Debian 8.0 (Jessie) systemd 开始破坏了与 System V init 的兼容性。

所以这里建议创建一个 systemd 服务。此处使用的解决方案如下所示:

[Unit]
Description=The te1 script

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/bin/true
ExecStop=/usr/local/bin/te1

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

该systemd服务需要被保存在/lib/systemd/system/te1.service和安装sudo systemctl enable te1