From 849bb66b5596e6f0414f00ce3e6a7925febc9134 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Sun, 19 Mar 2023 21:35:59 +0100 Subject: [PATCH] feat(script): Initial commit --- README.md | 22 +++++++++++++++++++++- arch-needs-restart.hook | 11 +++++++++++ arch-needs-restart.sh | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 arch-needs-restart.hook create mode 100755 arch-needs-restart.sh diff --git a/README.md b/README.md index 12f7d5f..4714fce 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,23 @@ # arch-needs-restart -A helper script to check which updated libraries and kernel are in use and require a restart \ No newline at end of file +A helper script to check which updated libraries and kernel are in use and require a restart + +# Setup + +Get started like so: + +1. Clone repo into arbitrary path `` +1. Make `needs-restart.sh` executable + ``` + chmod +x /needs-restart.sh + ``` +1. Symlink to files, for example + ``` + sudo ln -s /needs-restart.sh /usr/local/bin/arch-needs-restart + sudo ln -s /arch-needs-restart.hook /usr/share/libalpm/hooks/arch-needs-restart.hook + ``` + Note that while you may choose arbitrary locations for symlinks `arch-needs-restart.hook` references `/usr/local/bin/arch-needs-restart`. Change that accordingly if you need to. + +# Credits + +[StackExchange user Christian Zangl](https://unix.stackexchange.com/users/46158/laktak) aka `laktak` in January 2021 post [unix.stackexchange.com/a/630982](https://unix.stackexchange.com/a/630982) diff --git a/arch-needs-restart.hook b/arch-needs-restart.hook new file mode 100644 index 0000000..a72eb94 --- /dev/null +++ b/arch-needs-restart.hook @@ -0,0 +1,11 @@ +[Trigger] +Operation = Install +Operation = Upgrade +Operation = Remove +Type = Package +Target = * + +[Action] +Description = Check if anything's using outdated libs or kernel +When = PostTransaction +Exec = /usr/local/bin/arch-needs-restart diff --git a/arch-needs-restart.sh b/arch-needs-restart.sh new file mode 100755 index 0000000..c7b4949 --- /dev/null +++ b/arch-needs-restart.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +get_boot_kernel() { + local get_version=0 + for field in $(file /boot/vmlinuz*); do + if [[ $get_version -eq 1 ]]; then + echo $field + return + elif [[ $field == version ]]; then + # the next field contains the version + get_version=1 + fi + done +} + +rc=1 + +libs=$(lsof -n +c 0 2> /dev/null | grep 'DEL.*lib' | awk '1 { print $1 ": " $NF }' | sort -u) +if [[ -n $libs ]]; then + cat <<< $libs + echo "# LIBS: reboot required" + rc=0 +fi + +active_kernel=$(uname -r) +current_kernel=$(get_boot_kernel) +if [[ $active_kernel != $current_kernel ]]; then + echo "$active_kernel < $current_kernel" + echo "# KERNEL: reboot required" + rc=0 +fi +exit $rc