Bash 使用 rc.status 的脚本在较新的 SLES 上执行两次

Bash script using rc.status executing twice on newer SLES

我有一个奇怪的现象,当我们从 SLES 11 迁移到 SLES 12 (SuSE Enterprise Linux) 时,一个长期存在的管理脚本启动 运行ning 或多或少两次。我可以用一个最小的例子 test.sh:

来追踪它与 /etc/rc.status 的关系
#!/bin/bash
echo Sourcing rc.status
. /etc/rc.status
echo End of script

当这是 运行 且 status 作为参数时(我的脚本的常见用例)...

./test.sh status

...我观察到这个输出:

Sourcing rc.status
Sourcing rc.status
End of script
End of script

什么给了?

原来在 SLES /etc/rc.status 文件中有一些诗意:

user@host:~> diff rc.status.sles11 /etc/rc.status
34a35,92
> # Check if the service is used under systemd but not started with
> if test -z "$SYSTEMD_NO_WRAP" && /usr/bin/mountpoint -q /sys/fs/cgroup/systemd; then
>     if test $PPID -ne 1 -a $# -eq 1 ; then
>       _rc_base=
...

所以解决方案是在采购 /etc/rc.status 之前设置 SYSTEMD_NO_WRAP

#!/bin/bash
echo Sourcing rc.status
SYSTEMD_NO_WRAP=1
. /etc/rc.status
echo End of script

这给出了预期的行为:

Sourcing rc.status
End of script