feat(script): Initial commit

This commit is contained in:
hygienic-books 2023-03-19 21:35:59 +01:00
parent d8848c868e
commit 849bb66b55
3 changed files with 64 additions and 1 deletions

View File

@ -1,3 +1,23 @@
# arch-needs-restart # arch-needs-restart
A helper script to check which updated libraries and kernel are in use and require a restart 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 `<repo>`
1. Make `needs-restart.sh` executable
```
chmod +x <repo>/needs-restart.sh
```
1. Symlink to files, for example
```
sudo ln -s <repo>/needs-restart.sh /usr/local/bin/arch-needs-restart
sudo ln -s <repo>/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)

11
arch-needs-restart.hook Normal file
View File

@ -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

32
arch-needs-restart.sh Executable file
View File

@ -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