From 654b8891fd9beef2c4234e9d2cfad128ea5c92da Mon Sep 17 00:00:00 2001 From: Ehtesh Choudhury Date: Tue, 4 Jun 2024 00:10:11 -0700 Subject: [PATCH 1/2] use bkt to cache apt package count and simplify script --- update-motd-static.d/20-update | 53 ---------------------------------- update-motd.d/10-welcome | 0 update-motd.d/15-system | 0 update-motd.d/20-update | 28 ++++++++++++++++++ 4 files changed, 28 insertions(+), 53 deletions(-) delete mode 100644 update-motd-static.d/20-update mode change 100644 => 100755 update-motd.d/10-welcome mode change 100644 => 100755 update-motd.d/15-system mode change 100644 => 100755 update-motd.d/20-update diff --git a/update-motd-static.d/20-update b/update-motd-static.d/20-update deleted file mode 100644 index f7bd1cb..0000000 --- a/update-motd-static.d/20-update +++ /dev/null @@ -1,53 +0,0 @@ -#!/bin/bash - -function color (){ - echo "\e[$1m$2\e[0m" -} - -function setCountColor (){ - local input=$1 - countColor="38;5;16;48;5;242" - - if [ $input == 0 ]; then - countColor="38;5;16;48;5;242" - else - countColor="38;5;16;48;5;71" - fi -} - -function msgFormat (){ - local input=$1 - local packagesPlural="s" - - if [[ $input -eq 0 || $input -eq 1 ]]; then - packagesPlural="" - fi - echo "package$packagesPlural" -} - -msgColor="38;5;103" - -# Count -apt-get update --quiet=2 -pkgCount="$(apt-get -s dist-upgrade | grep -Po '^\d+(?= upgraded)')" -setCountColor "$pkgCount" - -# Message -msgHeader="$(color $msgColor \*)" -msgCount="$(color $countColor " $pkgCount ")" -msgLabel="$(color $msgColor "$(msgFormat $pkgCount) can be upgraded")" - -updateMsg=" $msgHeader $msgCount $msgLabel" - -# Output To Static Script -OUT="/etc/update-motd.d/"$(basename $0) -exec >${OUT} -echo "#!/bin/bash" -echo -echo "#####################################################" -echo "# DO NOT EDIT THIS SCRIPT #" -echo "# EDIT: /etc/update-motd-static.d/20-update #" -echo "#####################################################" -echo "cat < 0 )); then + countColor="38;5;16;48;5;71" +fi +if (( $pkgCount == 1 )); then + plurality="package" +fi + +# Message +msgHeader=$(color $msgColor \*) +msgCount=$(color $countColor " $pkgCount ") +msgLabel=$(color $msgColor "$plurality can be upgraded") + +echo -e "\n$msgHeader $msgCount $msgLabel\n" + From 9ca430c66715649a371220b95b10777418a225e8 Mon Sep 17 00:00:00 2001 From: Ehtesh Choudhury Date: Tue, 4 Jun 2024 00:10:43 -0700 Subject: [PATCH 2/2] change motd-update timer to run bkt on a daily interval --- systemd-timer/motd-update.service | 2 +- systemd-timer/motd-update.timer | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/systemd-timer/motd-update.service b/systemd-timer/motd-update.service index effe1cd..3c611e6 100644 --- a/systemd-timer/motd-update.service +++ b/systemd-timer/motd-update.service @@ -3,4 +3,4 @@ Description=Motd upgradable package count service After=network.target [Service] -ExecStart=run-parts /etc/update-motd-static.d +ExecStart=/bin/bash -c "bkt --ttl 1d --force -- sudo apt-get update --quiet=2 && bkt --ttl 1d --force -- sudo apt-get -s dist-upgrade" diff --git a/systemd-timer/motd-update.timer b/systemd-timer/motd-update.timer index 37f3597..e269f41 100644 --- a/systemd-timer/motd-update.timer +++ b/systemd-timer/motd-update.timer @@ -4,6 +4,6 @@ Description=Motd upgradable package count timer - Daily at 8:00PM [Timer] OnCalendar=*-*-* 20:00 Persistent=true - + [Install] WantedBy=timers.target