linux:mintupdate-on-wakeup
Differences
This shows you the differences between two versions of the page.
| linux:mintupdate-on-wakeup [2023/12/11 16:50] – created Wuff | linux:mintupdate-on-wakeup [2023/12/11 16:53] (current) – Wuff | ||
|---|---|---|---|
| Line 3: | Line 3: | ||
| v2 is available here: https:// | v2 is available here: https:// | ||
| + | Prerequisites: | ||
| + | < | ||
| + | sudo apt-get install at libnotify-bin | ||
| + | </ | ||
| + | | ||
| + | | ||
| + | <code bash mintupdate-on-wakeup.sh> | ||
| + | #!/bin/bash | ||
| + | ARGS=$1; SCRIPTDIR=${0%/ | ||
| + | ################################################################################ | ||
| + | # | ||
| + | # mintupdate-on-wakeup by tux@enkidu.eu | ||
| + | # | ||
| + | # (based on unattended-upgrades-alternative 1.1 by tux@enkidu.eu) | ||
| + | # | ||
| + | # 28.12.2018: v1.0 - initial release (Mint 18 only) | ||
| + | # 30.04.2021: v2.0 - added support for mintupdate-cli | ||
| + | # | ||
| + | ################################################################################ | ||
| + | AUTHOR=" | ||
| + | |||
| + | ############################################################################### | ||
| + | # | ||
| + | # This is a simple replacement for " | ||
| + | # It is NOT thought for server usage! | ||
| + | # It is thought for e.g. users with a notebook, where normal usage is | ||
| + | # open notebook = resume/ | ||
| + | # close notebook = hibernate/ | ||
| + | # | ||
| + | # Script is executed after each wakeup and update done 2-3 times a day. | ||
| + | # (Not at every resume, only if first digit of 2 digit hour changes. | ||
| + | # E.g. 0x (00, 01, 02, ...), 1x, 2x) | ||
| + | # | ||
| + | ################################################################################ | ||
| + | |||
| + | ################################################################################ | ||
| + | # | ||
| + | # >>> | ||
| + | # | ||
| + | ################################################################################ | ||
| + | |||
| + | ################################################################################ | ||
| + | # | ||
| + | # Installation: | ||
| + | # ------------- | ||
| + | # | ||
| + | # Place this script in $HOME/bin | ||
| + | # | ||
| + | # > chmod 755 $HOME/ | ||
| + | # > sudo ln -s $HOME/ | ||
| + | # | ||
| + | # | ||
| + | # If you want to check for updates after login, you have to add | ||
| + | # | ||
| + | # sudo $HOME/ | ||
| + | # | ||
| + | # to your | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # AND edit your sudoers config accordingly that it can be run without pw! | ||
| + | # | ||
| + | # | ||
| + | # IMPORTANT: Please see also configuration below! | ||
| + | # | ||
| + | ################################################################################ | ||
| + | |||
| + | ################################################################################ | ||
| + | # | ||
| + | # Usage: | ||
| + | # ------ | ||
| + | # | ||
| + | # For testing purposes you can invoke it like | ||
| + | # | ||
| + | # > sudo ./ | ||
| + | # | ||
| + | # | ||
| + | # To clean logs, index, tmp, start it with | ||
| + | # | ||
| + | # > sudo ./ | ||
| + | # | ||
| + | ################################################################################ | ||
| + | |||
| + | ################################################################################ | ||
| + | # | ||
| + | # See / | ||
| + | # | ||
| + | ################################################################################ | ||
| + | |||
| + | # | ||
| + | # | ||
| + | # User configuration | ||
| + | # | ||
| + | |||
| + | # If you are still on LM 18, use: | ||
| + | # Your mintupdate-tool settings (-r -y is added by default) | ||
| + | # See " | ||
| + | MUTOPTS=" | ||
| + | |||
| + | # From LM 19 on, use: | ||
| + | # Your mintupdate-cli settings (-r -y is added by default) | ||
| + | # See " | ||
| + | MUCOPTS="" | ||
| + | |||
| + | # if mintupdate-cli/ | ||
| + | OPENUPDGUIONERR=true | ||
| + | |||
| + | # Delay in minutes the script waits before mintupdate-cli/ | ||
| + | UPDATEDELAY=2 | ||
| + | |||
| + | |||
| + | # Desktop notifications | ||
| + | # | ||
| + | # Prerequisite: | ||
| + | # If not installed it will simply not notify! :) | ||
| + | |||
| + | # Level 0: disable desktop notifications | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # For normal usage, I suggest to use 2. | ||
| + | NOTIFY_LEVEL=5 | ||
| + | |||
| + | |||
| + | # The following variables only need to be changed if the values returend | ||
| + | # do not fit your needs! | ||
| + | |||
| + | |||
| + | # Icons used for notification. Change only if they do not fit or do not exist | ||
| + | NOTIFY_ICON_AVAIL="/ | ||
| + | NOTIFY_ICON_CHECK="/ | ||
| + | NOTIFY_ICON_ERROR="/ | ||
| + | NOTIFY_ICON_INSTALL="/ | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | # | ||
| + | |||
| + | |||
| + | # Here you may manually enter the value you get in a normal terminal when typing | ||
| + | # > echo $DISPLAY | ||
| + | # BUT ONLY change it, if the display could not properly be detected! | ||
| + | DESKTOP_DISPLAY=": | ||
| + | |||
| + | # What is the name of the desktop user you are working with? | ||
| + | # Some commands need to be invoked inside your user context or it does not work. | ||
| + | # BUT ONLY change it, if the user could not properly be detected! | ||
| + | DESKTOP_USER=$( who | grep ' | ||
| + | |||
| + | # | ||
| + | # End of user configuration | ||
| + | # | ||
| + | # | ||
| + | |||
| + | |||
| + | ################################################################################ | ||
| + | ################################################################################ | ||
| + | ## | ||
| + | ## DO NOT EDIT BELOW IF YOU DO NOT REALLY KNOW WHAT YOU ARE DOING!!! | ||
| + | ## | ||
| + | ################################################################################ | ||
| + | ################################################################################ | ||
| + | |||
| + | |||
| + | [ $USER != root ] && { | ||
| + | echo " | ||
| + | $SCRIPTNAME: | ||
| + | |||
| + | Usage: sudo $SCRIPTNAME < < | ||
| + | " | ||
| + | exit 1 | ||
| + | } | ||
| + | |||
| + | log() { | ||
| + | # $1: log message level | ||
| + | # 2: text | ||
| + | # 3: additional text | ||
| + | |||
| + | DT=$( date +' | ||
| + | printf "%s | %5d | %d - %s\n" " | ||
| + | [ ! -z " | ||
| + | |||
| + | |||
| + | # send desktop notification | ||
| + | [ -z " | ||
| + | (( NOTIFY_LEVEL < $1 )) && return | ||
| + | |||
| + | case $1 in | ||
| + | 1) ICO=$NOTIFY_ICON_ERROR; | ||
| + | 2) ICO=$NOTIFY_ICON_INSTALL; | ||
| + | 3) ICO=$NOTIFY_ICON_AVAIL; | ||
| + | 4) ICO=$NOTIFY_ICON_CHECK; | ||
| + | 5) ICO=$NOTIFY_ICON_CHECK; | ||
| + | esac | ||
| + | |||
| + | IC='' | ||
| + | [ -r $ICO ] && IC="-i $ICO" || echo >&2 " | ||
| + | |||
| + | BODY=" | ||
| + | [ ! -z " | ||
| + | |||
| + | TITLE=$( printf " | ||
| + | |||
| + | # notify-send -a $SCRIPTNAME $IC -u $URG " | ||
| + | su -c " | ||
| + | } | ||
| + | |||
| + | quit() { | ||
| + | log 5 "*** $SCRIPTNAME ended ***" | ||
| + | echo | ||
| + | exit $1 | ||
| + | } | ||
| + | |||
| + | tfunc() { | ||
| + | type $1 | sed -n ' | ||
| + | } | ||
| + | |||
| + | |||
| + | # some presets | ||
| + | LOG_FILE=/ | ||
| + | |||
| + | # what mintupdate to use? | ||
| + | which mintupdate-cli 2>&1 >/ | ||
| + | MUCMD=mintupdate-cli | ||
| + | UPDCMD=" | ||
| + | } || { | ||
| + | MUCMD=mintupdate-tool | ||
| + | UPDCMD=" | ||
| + | } | ||
| + | |||
| + | CHECKUPDRUNNING=" | ||
| + | NOTIFY_SEND=${NOTIFY_SEND: | ||
| + | which $NOTIFY_SEND >/ | ||
| + | |||
| + | # get DBUS of user (it is tricky and might not work for all...) | ||
| + | [ -z " | ||
| + | DUID=$( pgrep -l " | ||
| + | DSBA=$( grep -z ' | ||
| + | export DBUS_SESSION_BUS_ADDRESS=${DSBA# | ||
| + | } 2>/ | ||
| + | |||
| + | # check DISPLAY | ||
| + | [ -z " | ||
| + | export DISPLAY=" | ||
| + | |||
| + | LASTRUN_INDEX=/ | ||
| + | TMP_FILE=/ | ||
| + | |||
| + | |||
| + | # redirect all output to logfile | ||
| + | exec >> | ||
| + | exec 2>&1 | ||
| + | |||
| + | |||
| + | log 5 "*** $SCRIPTNAME v$VER by $AUTHOR started ***" "[ \$1=' | ||
| + | |||
| + | # is " | ||
| + | which at >/ | ||
| + | log 1 ' | ||
| + | quit 2 | ||
| + | } | ||
| + | |||
| + | [ -z " | ||
| + | log 2 " | ||
| + | " | ||
| + | |||
| + | case " | ||
| + | post|resume|thaw|login) | ||
| + | log 4 "## Wakeup ($ARGS)" | ||
| + | |||
| + | # read last run | ||
| + | LASTRUN=`cat $LASTRUN_INDEX 2>/ | ||
| + | NOW=`date " | ||
| + | |||
| + | # Skip, if update already in progress! | ||
| + | if (( `eval " | ||
| + | log 3 " | ||
| + | # Skip update if in same hour range | ||
| + | elif (( LASTRUN == NOW )); then | ||
| + | log 3 " | ||
| + | # Check & install updates | ||
| + | else | ||
| + | # run this part in background, otherwise it will conflict with other wakeup scripts! | ||
| + | cat > $TMP_FILE << | ||
| + | #!/bin/bash | ||
| + | trap "" | ||
| + | LOG_FILE=$LOG_FILE | ||
| + | SCRIPTNAME=$SCRIPTNAME | ||
| + | exec >> | ||
| + | exec 2>&1 | ||
| + | export DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS | ||
| + | export DISPLAY=" | ||
| + | `set | egrep ' | ||
| + | `set | egrep ' | ||
| + | `tfunc log` | ||
| + | `tfunc quit` | ||
| + | log 5 " | ||
| + | |||
| + | # check internet connection first | ||
| + | log 4 " | ||
| + | I=0 | ||
| + | while true | ||
| + | do | ||
| + | (( I > 20 )) && { | ||
| + | log 1 " | ||
| + | quit 1 | ||
| + | } | ||
| + | |||
| + | let I=I+1 | ||
| + | |||
| + | DG=\$( ip r | fgrep default | awk '{ print \$3; }' ) | ||
| + | [ -z " | ||
| + | log 5 "> No default gateway found, waiting 10sec for interface..." | ||
| + | sleep 10 | ||
| + | continue | ||
| + | } | ||
| + | |||
| + | ping -c 1 -q -W 3 \$DG || { | ||
| + | log 5 "> default gateway ' | ||
| + | sleep 10 | ||
| + | continue | ||
| + | } | ||
| + | |||
| + | ping -c 1 -q -W 3 www.google.com || { | ||
| + | log 5 "> www.google.com is not reachable, waiting 10sec to try again..." | ||
| + | sleep 10 | ||
| + | continue | ||
| + | } | ||
| + | |||
| + | break | ||
| + | done | ||
| + | log 4 ">> | ||
| + | |||
| + | log 3 " | ||
| + | $UPDCMD \ | ||
| + | && | ||
| + | || { | ||
| + | log 1 "> $MUCMD returned error \$?!" ">> | ||
| + | [ " | ||
| + | && echo "su -c ' | ||
| + | } | ||
| + | echo > | ||
| + | quit 0 | ||
| + | __EOT__ | ||
| + | log 5 " | ||
| + | chmod +x $TMP_FILE | ||
| + | echo $TMP_FILE $$ | at -M now + $UPDATEDELAY min | ||
| + | # Using at was the only way I could find that my child process was not killed | ||
| + | # (nor disown & or hohup & did survive - dunno why!?) or caused strange | ||
| + | # side effects. Using at everything works perfect! :) | ||
| + | fi | ||
| + | ;; | ||
| + | |||
| + | pre|hibernate|suspend) | ||
| + | log 4 "## Suspend ($ARGS)" | ||
| + | # if going in hibernate/ | ||
| + | # But even if, it should be continued after resume... | ||
| + | # So no complicated hibernate handling is required. | ||
| + | |||
| + | let I=0 | ||
| + | while (( `eval " | ||
| + | do | ||
| + | (( I == 10 )) && break | ||
| + | (( ! I )) && log 2 "> Running updates detected!" | ||
| + | (( I )) && log 4 "> ... $I" | ||
| + | sleep 10 | ||
| + | let I=I+1 | ||
| + | done | ||
| + | (( ! I )) && log 5 "> No running updates detected." | ||
| + | quit 0 | ||
| + | ;; | ||
| + | |||
| + | cleanup) | ||
| + | rm -f $LASTRUN_INDEX $LOG_FILE $TMP_FILE >/ | ||
| + | log 4 ">> | ||
| + | ;; | ||
| + | |||
| + | *) log 1 "Mode ' | ||
| + | quit 1 | ||
| + | ;; | ||
| + | esac | ||
| + | </ | ||
| + | |||
| + | |||
| + | Version 1 for reference: | ||
| <code bash mintupdate-on-wakeup.sh> | <code bash mintupdate-on-wakeup.sh> | ||
linux/mintupdate-on-wakeup.1702313454.txt.gz · Last modified: by Wuff