File autoupdate of Package kimi-utils-ubuntu

#!/usr/bin/bash
# -*- mode: Shell-script; sh_shell: "bash"; -*-

# Force anacron to run: sudo run-parts /etc/cron.daily
# apt-get update
# apt-get upgrade -y
# apt-get dist-upgrade -y
# apt autoremove -y
# apt autoclean
# apt clean
#
# Check status with:
# journalctl -t uppdatera
# Real-time continuous checking in terminal: 
# journalctl -f -t uppdatera

# Show notification for active user
function notify-send() {
    #Detect the name of the display in use
    local display=":$(ls /tmp/.X11-unix/* | sed 's#/tmp/.X11-unix/X##' | head -n 1)"

    #Detect the user using such display
    local user=$(who | grep '('$display')' | awk '{print $1}' | head -n 1)

    #Detect the id of the user
    local uid=$(id -u $user)

    sudo -u $user DISPLAY=$display DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/$uid/bus notify-send "$@"
}

# Function to check internet connectivity
check_google_dns() {
    local dns_server=$1
    ping -q -w 1 -c 1 $dns_server > /dev/null 2>&1
    return $?  # Return the exit status of the ping command
}

# Check if default gateway is reachable
logger -t uppdatera Testar om default gateway är nåbar
if ping -q -w 1 -c 1 `ip r | grep default | cut -d ' ' -f 3` > /dev/null 2>&1; then
    logger -t uppdatera Default gateway är online
    # Check if Ubuntu package server is reachable
    logger -t uppdatera Testar om Ubuntu paketserver är nåbar
    echo -e "GET /ubuntu/ HTTP/1.0\r\nHost: archive.ubuntu.com\r\n\r\n" | nc archive.ubuntu.com 80 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
	logger -t uppdatera Ubuntu paketserver är online
    else
	logger -t uppdatera Ubuntu paketserver är offline
	# Check availability of Google's public DNS servers
	logger -t uppdatera Kontrollera Google DNS-servrar
	if check_google_dns "8.8.8.8"; then
	    logger -t uppdatera Internet är tillgänglig, men Ubuntu paketserver är nere.
	elif check_google_dns "8.8.4.4"; then
	    logger -t uppdatera Internet är tillgänglig, men Ubuntu paketserver är nere.
	else
            logger -t uppdatera Ingen internetanslutning.
            logger -t uppdatera Automatiserad systemuppdatering avbruten
            exit 0
	fi
    fi
else
    # Not all gateways respond with ping reply.
    # The alternative is to check ping from a well known server,
    # such as google 8.8.8.8
    logger -t uppdatera Default gateway svarar inte
    # Check if Ubuntu package server is reachable
    logger -t uppdatera Testar om Ubuntu paketserver är nåbar
    echo -e "GET /ubuntu/ HTTP/1.0\r\nHost: archive.ubuntu.com\r\n\r\n" | nc archive.ubuntu.com 80 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        logger -t uppdatera Ubuntu paketserver är online
    else
        logger -t uppdatera Ubuntu paketserver är offline
        # Check Google DNS servers
        logger -t uppdatera Kontrollera Google DNS-servrar
        if check_google_dns "8.8.8.8"; then
            logger -t uppdatera Internet är tillgänglig, men Ubuntu paketserver är nere.
        elif check_google_dns "8.8.4.4"; then
            logger -t uppdatera Internet är tillgänglig, men Ubuntu paketserver är nere.
            logger -t uppdatera Automatiserad systemuppdatering avbruten
            exit 0
        else
            logger -t uppdatera Ingen internetanslutning.
            logger -t uppdatera Automatiserad systemuppdatering avbruten
            exit 0
        fi
    fi
fi

logger -t uppdatera Automatiserad systemuppdatering startar
notify-send "Uppdaterar" -i gtk-dialog-warning -t 30000 "Stäng INTE av burken"
systemd-inhibit --why="Uppdaterar systemet" /usr/local/bin/update-ubuntu
DEBIAN_FRONTEND=noninteractive needrestart -u 'NeedRestart::UI::stdio' -r a
if [ -f /var/run/reboot-required ]; then
    notify-send "Boota om" -i system-reboot -t 30000 "Uppdateringen klar. Kräver omstart."
else
    notify-send "Uppdatering klar" -i gtk-ok -t 30000 "Öppna program kan behöva omstart."
fi
logger -t uppdatera Automatiserad systemuppdatering slutförd
exit 0
openSUSE Build Service is sponsored by