Commit 9c754163 authored by Ilja's avatar Ilja
Browse files

Adding logfile

parent 84c71c44
#!/bin/bash
#!/bin/bash
# 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.
# Each message is also kept in a logfile. You can check those also when using cron
# Make sure we have a password pop-up screen
################################################################
# General SETTINGS
################################################################
LOGLINES_TO_KEEP=30
MY_PASSWORD_DIALOG_FOLDER="$HOME/.local/bin"
MY_PASSWORD_DIALOG_FILE="my-password-dialog"
LOGFILE_DIR="$HOME/.update-system/log"
LOGFILE_NAME="update-system.log"
################################################################
# 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
##
my_password_dialog="$MY_PASSWORD_DIALOG_FOLDER/$MY_PASSWORD_DIALOG_FILE"
if [ ! -e $my_password_dialog ] && [ -z $SUDO_ASKPASS ]
then
......@@ -13,30 +51,42 @@ then
touch $my_password_dialog
chmod 700 $my_password_dialog
echo '#!/bin/bash' > $my_password_dialog
echo 'kdialog --password "sudo password required"' >> $my_password_dialog
echo 'kdialog --password "sudo password required"' > $my_password_dialog
fi
if [ -z $SUDO_ASKPASS ]
then
export SUDO_ASKPASS=$my_password_dialog
fi
# 1. Check how many updates there are
notify-send "Checking for updates..."
sudo -A apt-get update
##
# Check how many updates there are
# ... I don't have to do anything because it already happens for us
# I case I ever need it; set cronjob to automatically update the system
# The following should work, but is untested
#if [ ! -e /etc/cron.daily/apt-update.sh ]
#then
# sudo -A touch /etc/cron.daily/apt-update.sh
# sudo -A echo "apt-get update" > /etc/cron.daily/apt-update.sh
# chmod 100 /etc/cron.daily/apt-update.sh
#fi
##
# 2. If zero updates => exit properly
##
# If zero updates => don't upgrade
# Otherwise => do upgrade
##
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
notify-send "There are no updates. Awesome! Your system is up to date!"
exit 0;
log_info "There are no updates. Awesome! Your system is up to date!"
else
log_info "There are $number_of_upgrades updates. Starting update..."
sudo -A apt-get upgrade -y
sudo -A apt autoremove -y
log_info "Updating finished!"
fi
# 3. Otherwise, do upgrade
notify-send "There are $number_of_upgrades updates. Starting update..."
# Then upgrade and autoremove
sudo -A apt-get upgrade -y
sudo -A apt autoremove -y
# 5. Send notif that it's done
notify-send "Updating finished!"
##
# Clean up logfiles
##
echo "$(tail -n $LOGLINES_TO_KEEP ""$logfile"")" > "$logfile"
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment