Search code examples
bashsles

Bash script using rc.status executing twice on newer SLES


I have the strange phenomenon that a long-standing admin script started running more or less twice when we moved from SLES 11 to SLES 12 (SuSE Enterprise Linux). I could track this down to being related to /etc/rc.status with a minimal example test.sh:

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

When this is run with status as parameter (a common use-case for my script)...

./test.sh status

...I observe this output:

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

What gives?


Solution

  • Turns out there's been some poettering in the SLES /etc/rc.status file:

    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=
    ...
    

    So the solution is to set SYSTEMD_NO_WRAP before sourcing /etc/rc.status.

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

    This gives the expected behaviour:

    Sourcing rc.status
    End of script