File zypper-full-upgrade+restarts.bash of Package needrestart

#!/usr/bin/bash

# Ensure /sbin/ | /usr/sbin/ is in the BINARY $PATH for any $USER
export PATH=$PATH:/usr/sbin/

# Ensure the system file with OS information exists.
if [ -f /etc/os-release ]; then
    # Source the OS information so that variables like NAME can be used.
    echo ""
    echo "Checking for Appropriate SUSE Distribution . . ."
    echo ""
    . /etc/os-release
else
    echo "Error: [/etc/os-release] File not Found. Are You Running a Compatible SUSE Distribution?"
    exit 1
fi

if [[ "$NAME" == *"Leap"* ]]; then
    echo "Detected openSUSE Leap."
    echo ""
    echo "Committing Interactive System-Wide Upgrade ..."
    echo ""
    sudo zypper refresh --force
    sudo zypper update  --allow-arch-change --allow-downgrade --allow-name-change --allow-vendor-change --recommends --with-interactive --details
    sudo zypper patch   --allow-arch-change --allow-downgrade --allow-name-change --allow-vendor-change --recommends --with-interactive --with-optional --with-update --details
    echo ""

    echo ""
    echo "Checking for Outdated Libraries, Processes and Services . . ."
    echo ""
    # Capture needs-restarting output (processes)
    needs_restarting_output=$(sudo needs-restarting 2>&1)
    echo "Processes using outdated or deleted files (via needs-restarting) ..."
    echo ""
    echo "$needs_restarting_output"
    echo ""
    # Capture needrestart output (services)
    if ! needrestart_output=$(sudo needrestart -m a -r l 2>&1); then
        echo "Error: needrestart failed. Please check installation."
        exit 1
    fi
    echo "Services needing restart (via needrestart):"
    echo "$needrestart_output"
    echo ""

    # Check if needrestart indicates services need restarting
    if [[ -z "$needrestart_output" || "$needrestart_output" =~ "No services need to be restarted" ]]; then
        # No services to restart; check needs-restarting for processes
        if [[ -z "$needs_restarting_output" || ! "$needs_restarting_output" =~ [0-9]+\ [^\ ]+ ]]; then
            echo "No services or processes require a restart. Skipping restart prompt."
            echo ""
        else
            echo "No services require a restart, but some processes are using outdated or deleted files."
            echo "Consider manually restarting these processes or rebooting the system."
            echo ""
        fi
    else
        # Services need restarting; prompt for restart mode
        echo ""
        echo "Select Restart Mode for Services Requiring Updates:"
        echo "  [i] Interactive Mode: Prompt for Each Service Restart"
        echo "  [a] Automatic Mode: Restart Services Automatically Without Prompting"
        echo "  [s] Skip Mode: List Services Without Performing Restarts"
        echo -n "Enter Choice (i/a/s, default: i): "
        read restart_mode
        echo ""

        case "${restart_mode,,}" in
            a)
                echo "Executing Automatic Daemon/Service Restarts after Library Updates/Changes:"
                echo ""
                sudo needrestart -q -m a -r a
                ;;
            s)
                echo "Skipping Daemon/Service Restarts. Refer to the Above Listings for Services and Processes Requiring Updates."
                echo ""
                ;;
            i|""|*)
                echo "Executing Interactive Daemon/Service Restarts after Library Updates/Changes:"
                echo ""
                sudo needrestart -q -m a -r i
                ;;
        esac
    fi

    # Check if a reboot is recommended
    reboot_output=$(sudo needs-restarting -r 2>&1)
    if [[ $? -eq 1 || ! "$reboot_output" =~ "Reboot is probably not necessary" ]]; then
        echo ""
        echo "Note: A full system reboot is recommended due to updates (e.g., kernel or core libraries)."
    fi

    exit 0

elif [[ "$NAME" == *"Tumbleweed"* ]]; then
    echo "Detected openSUSE Tumbleweed."
    echo ""
    echo "Committing Interactive System-Wide Upgrade:"
    echo ""
    sudo zypper refresh      --force
    sudo zypper dist-upgrade --allow-arch-change --allow-downgrade --allow-name-change --allow-vendor-change --recommends --details
    sudo zypper patch        --allow-arch-change --allow-downgrade --allow-name-change --allow-vendor-change --recommends --with-interactive --with-optional --with-update --details
    echo ""

    echo ""
    echo "Checking for Outdated Libraries, Processes and Services . . ."
    echo ""
    # Capture needs-restarting output (processes)
    needs_restarting_output=$(sudo needs-restarting 2>&1)
    echo "Processes using outdated or deleted files (via needs-restarting) ..."
    echo ""
    echo "$needs_restarting_output"
    echo ""
    # Capture needrestart output (services)
    if ! needrestart_output=$(sudo needrestart -m a -r l 2>&1); then
        echo "Error: needrestart failed. Please check installation."
        exit 1
    fi
    echo "Services needing restart (via needrestart):"
    echo "$needrestart_output"
    echo ""

    # Check if needrestart indicates services need restarting
    if [[ -z "$needrestart_output" || "$needrestart_output" =~ "No services need to be restarted" ]]; then
        # No services to restart; check needs-restarting for processes
        if [[ -z "$needs_restarting_output" || ! "$needs_restarting_output" =~ [0-9]+\ [^\ ]+ ]]; then
            echo "No services or processes require a restart. Skipping restart prompt."
            echo ""
        else
            echo "No services require a restart, but some processes are using outdated or deleted files."
            echo "Consider manually restarting these processes or rebooting the system."
            echo ""
        fi
    else
        # Services need restarting; prompt for restart mode
        echo ""
        echo "Select Restart Mode for Services Requiring Updates:"
        echo "  [i] Interactive Mode: Prompt for Each Service Restart"
        echo "  [a] Automatic Mode: Restart Services Automatically Without Prompting"
        echo "  [s] Skip Mode: List Services Without Performing Restarts"
        echo -n "Enter Choice (i/a/s, default: i): "
        read restart_mode
        echo ""

        case "${restart_mode,,}" in
            a)
                echo "Executing Automatic Daemon/Service Restarts after Library Updates/Changes:"
                echo ""
                sudo needrestart -q -m a -r a
                ;;
            s)
                echo "Skipping Daemon/Service Restarts. Refer to the Above Listings for Services and Processes Requiring Updates."
                echo ""
                ;;
            i|""|*)
                echo "Executing Interactive Daemon/Service Restarts after Library Updates/Changes:"
                echo ""
                sudo needrestart -q -m a -r i
                ;;
        esac
    fi

    # Check if a reboot is recommended
    reboot_output=$(sudo needs-restarting -r 2>&1)
    if [[ $? -eq 1 || ! "$reboot_output" =~ "Reboot is probably not necessary" ]]; then
        echo ""
        echo "Note: A full system reboot is recommended due to updates (e.g., kernel or core libraries)."
    fi

    exit 0

else
    echo "Unsupported openSUSE Distribution: $NAME"
    exit 1
fi
openSUSE Build Service is sponsored by