"uname" will tell me the version of the running kernel. How do I know if this is different from the installed kernel (i.e. the one that will be loaded on the next reboot)?
Ideally I'd like to write a script that can output "reboot needed" or "running and installed kernel match".
Use
freebsd-version -k
! From its manual:You should compare output of
freebsd-version -k
anduname -r
and if differs you shouldreboot
.Another option is to use
strings
(idea taken from/bin/freebsd-version
):Simple, but probably not reliable way: compare boot time (from
sysctl kern.boottime
) and time when kernel was installed (stat -f '%c' $(sysctl -n kern.bootfile)
). Time should be always synced (via ntp) for this method to work.Better way is to compare version strings from running kernel and installed kernel file.
From running kernel it can be obtained via
sysctl -n kern.version | head -1
(this string is also shown in uname -v but in one line instead two lines). Then you canfgrep
this string in the kernel file (kern.bootfile
) - if it found - running and installed kernel version should match. I don't like using grep here, but it should work good enough. More correct way of extracting version string from kernel file would be some program to extractversion
variable from kernel ELF data (e. g. using libelf).