update-system.sh 2.68 KB
Newer Older
Ilja's avatar
Ilja committed
1
#!/bin/bash
2
3
4

# This script updates the system and sends a notification when updates start and stops.
# Note that notifications wont work when using cron, only if the script is started in the desktop itself.
Ilja's avatar
Ilja committed
5
# Each message is also kept in a logfile.
Ilja's avatar
Ilja committed
6
7
8
9
10

################################################################
# General SETTINGS
################################################################
LOGLINES_TO_KEEP=30
11
12
MY_PASSWORD_DIALOG_FOLDER="$HOME/.local/bin"
MY_PASSWORD_DIALOG_FILE="my-password-dialog"
Ilja's avatar
Ilja committed
13
14
LOGFILE_DIR="$HOME/.update-system/log"
LOGFILE_NAME="update-system.log"
Ilja's avatar
Ilja committed
15
EXCLUDE_IF_RUNNING="firefox"
Ilja's avatar
Ilja committed
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

################################################################
# FUNCTIONS
################################################################
function log_info() {
    message=$1
    echo "$(date --rfc-3339='seconds' -u)  $message" >> "$logfile"
    notify-send "$message"
}

################################################################
# ACTUAL SCRIPT
################################################################

##
# Make sure we have a logfile
##
logfile="$LOGFILE_DIR/$LOGFILE_NAME"
if [ ! -e $logfile ]
then
    mkdir -p "$LOGFILE_DIR"
    touch "$logfile"
fi

log_info "Checking for updates..."

##
# Make sure we have a password pop-up screen
##
45
46
47
48
49
50
51
my_password_dialog="$MY_PASSWORD_DIALOG_FOLDER/$MY_PASSWORD_DIALOG_FILE"
if [ ! -e $my_password_dialog ] && [ -z $SUDO_ASKPASS ]
then
    mkdir -p $MY_PASSWORD_DIALOG_FOLDER
    touch $my_password_dialog
    chmod 700 $my_password_dialog
    echo '#!/bin/bash' > $my_password_dialog
Ilja's avatar
Ilja committed
52
    echo 'kdialog --password "sudo password required"' > $my_password_dialog
53
54
55
56
57
fi
if [ -z $SUDO_ASKPASS ]
then
    export SUDO_ASKPASS=$my_password_dialog
fi
Ilja's avatar
Ilja committed
58

Ilja's avatar
Ilja committed
59
60
##
# Check how many updates there are
Ilja's avatar
Ilja committed
61
62
# * If zero updates => don't upgrade
# * Otherwise       => do upgrade
Ilja's avatar
Ilja committed
63
##
Ilja's avatar
Ilja committed
64
sudo -A apt-get update
Ilja's avatar
Ilja committed
65
66
67
number_of_upgrades=$(LC_ALL=C apt-get upgrade -s | grep -E "[0-9]+ upgraded, .*" | sed -E "s/ .*//g")
if [ $number_of_upgrades -eq 0 ];
then
Ilja's avatar
Ilja committed
68
69
70
    log_info "There are no updates. Awesome! Your system is up to date!"
else
    log_info "There are $number_of_upgrades updates. Starting update..."
Ilja's avatar
Ilja committed
71
72
73
74
75
76
77
78
79

    RUNNING=""
    for prog in $EXCLUDE_IF_RUNNING
    do
      ! [ -z $(pgrep $prog) ] && RUNNING="$prog $RUNNING"
    done
    HOLDING=$(apt-mark showhold | tr '\n' ' ')
    ! [ -z "$RUNNING" ] && sudo -A apt-mark hold $RUNNING && log_info "Won't be upgraded becasue they are active: $RUNNING"

Ilja's avatar
Ilja committed
80
81
    sudo -A apt-get upgrade -y
    sudo -A apt autoremove -y
Ilja's avatar
Ilja committed
82
83
84
85

    ! [ -z "$RUNNING" ] && sudo -A apt-mark unhold $RUNNING
    ! [ -z "$HOLDING" ] && sudo -A apt-mark hold $HOLDING

Ilja's avatar
Ilja committed
86
    log_info "Updating finished!"
Ilja's avatar
Ilja committed
87
88
fi

Ilja's avatar
Ilja committed
89
90
91
92
##
# Clean up logfiles
##
echo "$(tail -n $LOGLINES_TO_KEEP ""$logfile"")" > "$logfile"