menu script for systemctl timer

Script checks for systemctl state loaded and state activated. Choices diffrent based on state
mail@pastecode.io avatar
unknown
sh
a month ago
8.9 kB
5
Indexable
Never
#!/bin/bash

source ~/steammenuscripts/paramsysctl.soulmask
loaded=$(sudo systemctl status autoupdatesteamgs1.timer | grep -Po '(?<=Loaded: )\w+(?= \()')
active=$(sudo systemctl status autoupdatesteamgs1.timer | grep -Po '(?<=Active: )\w+(?= \()')
timestamp=$(date +"%Y-%m-%d_%H-%M-%S")
servicestatus=""

# Use a case statement to set the variable based on the output
case $loaded in
    masked)
        servicestatus="Disabled (masked)"
        ;;
    loaded)
        servicestatus="Enabled (unmasked)"
        ;;
    *)
        echo "Unexpected output: $output"
        exit 1
        ;;
esac
# Initial message box with OK button
whiptail --title "Information" --msgbox "$(systemctl status autoupdatesteamgs1.timer --no-pager)" 0 0

# Yes/No box
if whiptail --title "Confirmation" --yesno "Autoupdate is $servicestatus and $active ; do you wish to change this ?" 10 60 --defaultno; then

sync
while true; do
    if [[ "$loaded" == "masked" ]]; then
        CHOICE=$(whiptail --separate-output --radiolist "AUTOUPDATE IS DISABLED, DO YOU WISH TO..." 0 0 5 \
            "1" "Do nothing and exit" ON \
            "2" "Enable Autoupdate and also activate" OFF \
            "3" "Enable Autoupdate but don't activate until next restart" OFF 3>&1 1>&2 2>&3)

        if [ -z "$CHOICE" ]; then
            echo "No option was chosen (user hit Cancel)"
        else
##            echo "The user chose $CHOICE"
            case $CHOICE in
                "1")
                    exit
                    ;;
                "2")
                    {
                    sleep 0.5
                    echo -e "XXX\n0\nEnabling autoupdate timer... \nXXX"
                    sudo systemctl unmask $sysctltimer &> /dev/null
                    sleep 2
                    echo -e "XXX\n33\nEnabling autoupdate timer... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n33\nActivating autoupdate timer... \nXXX"
                    sudo systemctl start $sysctltimer
                    sleep 2
                    echo -e "XXX\n66\nActivating autoupdate timer... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n66\nWriting to log... \nXXX"
                    echo "Autoupdate enabled (unmasked) and activated $timestamp" >> ~/steammenuscripts/serverlog
                    sleep 2
                    echo -e "XXX\n100\nWriting to log\nXXX"

                    } | whiptail --title "$gamename" --gauge "Enabling autoupdate" 6 60 0
                    exit
                    ;;
                "3")
                    {
                    sleep 0.5
                    echo -e "XXX\n0\nEnabling autoupdate timer... \nXXX"
                    sudo systemctl unmask $sysctltimer &> /dev/null
                    sleep 3
                    echo -e "XXX\n33\nEnabling autoupdate timer... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n33\nWriting to log file... \nXXX"
                    echo "Autoupdate enabled (unmasked) not activated $timestamp" >> ~/steammenuscripts/serverlog
                    sleep 1
                    echo -e "XXX\n66\nWriting to log file... Done.\nXXX"
                    sleep 0.5

                    } | whiptail --title "$gamename" --gauge "Enabling autoupdate..." 6 60 0
                    exit
                    ;;
                  *)
                    echo "Invalid option."
                    ;;
            esac
        fi
        continue

    elif [[ "$loaded" == "loaded" ]]; then
        if [[ "$active" == "inactive" ]]; then
            CHOICE=$(whiptail --separate-output --radiolist "AUTOUPDATE IS ENABLED BUT INACTIVE, DO YOU WISH TO..." 0 0 5 \
                "1" "Do nothing and exit" ON \
                "2" "Activate autoupdate timer" OFF \
                "3" "Disable (mask) Autoupdate" OFF 3>&1 1>&2 2>&3)

            if [ -z "$CHOICE" ]; then
                echo "No option was chosen (user hit Cancel)"
            else
                echo "The user chose $CHOICE"
                case $CHOICE in
                    "1")
                        exit
                        ;;
                    "2")
                        {
                    sleep 0.5
                    echo -e "XXX\n0\nActivated autoupdate timer... \nXXX"
                    sudo systemctl start $sysctltimer
                    sleep 2
                    echo -e "XXX\n33\nActivating autoupdate timer... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n33\nWriting to log file... \nXXX"
                    echo "Autoupdate activated $timestamp" >> ~/steammenuscripts/serverlog
                    sleep 2
                    echo -e "XXX\n66\nWriting to log file... Done.\nXXX"
                    sleep 0.5

                    } | whiptail --title "$gamename" --gauge "Activating autoupdate" 6 60 0
                    exit
                    ;;
                    "3")
                        {
                    sleep 0.5
                    echo -e "XXX\n0\nStopping autoupdate timer... \nXXX"
                    sudo systemctl stop $sysctltimer
                    sleep 2
                    echo -e "XXX\n33\nStopping autoupdate timer... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n33\nDisabling autoupdate timer... \nXXX"
                    sudo systemctl mask $sysctltimer &> /dev/null
                    sleep 2
                    echo -e "XXX\n66\nDisabling autoupdate timer... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n66\nWriting to log... \nXXX"
                    echo "Autoupdate disabled (masked) $timestamp" >> ~/steammenuscripts/serverlog
                    sleep 2
                    echo -e "XXX\n100\nWriting to log\nXXX"

                    } | whiptail --title "$gamename" --gauge "Disabling autoupdate" 6 60 0
                    exit
                        ;;
                    *)
                        exit
                        ;;
                esac
            fi
        elif [[ "$active" == "active" ]]; then
            CHOICE=$(whiptail --separate-output --radiolist "AUTOUPDATE IS ENABLED AND ACTIVE, DO YOU WISH TO..." 0 0 5 \
                "1" "Do nothing and exit" ON \
                "2" "Deactivate autoupdate until next restart" OFF \
                "3" "Deactivate and disable (mask) Autoupdate" OFF 3>&1 1>&2 2>&3)

            if [ -z "$CHOICE" ]; then
                echo "No option was chosen (user hit Cancel)"
            else
                echo "The user chose $CHOICE"
                case $CHOICE in
                    "1")
                        exit
                        ;;
                    "2")
                         {
                    sleep 0.5
                    echo -e "XXX\n0\nDeactivate autoupdate timer... \nXXX"
                    sudo systemctl stop $sysctltimer
                    sleep 2
                    echo -e "XXX\n33\nDeactivate autoupdate timer... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n33\nWriting to log file... \nXXX"
                    echo "Autoupdate deactivated $timestamp" >> ~/steammenuscripts
                    sleep 2
                    echo -e "XXX\n66\nWriting to log file... Done.\nXXX"
                    sleep 0.5

                    } | whiptail --title "$gamename" --gauge "Deactivating autoupdate" 6 60 0
                    exit
                        ;;
                    "3")
                        {
                    sleep 0.5
                    echo -e "XXX\n0\nDeactivating autoupdate timer... \nXXX"
                    sudo systemctl stop $sysctltimer
                    sleep 2
                    echo -e "XXX\n33\nDeactivating autoupdate timer... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n33\nDisabling (mask) autoupdate... \nXXX"
                    sudo systemctl mask $sysctltimer &> /dev/null
                    sleep 2
                    echo -e "XXX\n66\nDisabling (mask) autoupdate... Done.\nXXX"
                    sleep 0.5

                    echo -e "XXX\n66\nWriting to log... \nXXX"
                    echo "Autoupdate disabled (masked) $timestamp" >> ~/steammenuscripts/serverlog
                    sleep 2
                    echo -e "XXX\n100\nWriting to log... Done.\nXXX"

                    } | whiptail --title "$gamename" --gauge "Disabling autoupdate" 6 60 0
                    exit
                        ;;
                    "4")
                        exit
                        ;;
                    *)
                        echo "Invalid option."
                        ;;
                esac
            fi
        fi
    else
        whiptail --title "Information" --msgbox "FAILURE: Please contact server administrator for systemctl reset" 0 0
        sync
        exit
    fi
    sleep 10
  done
else
exit
fi
Leave a Comment