This script will notify you if a reboot is required after automatic updates

Forum rules
Before you post please read how to get help
Post Reply
1337haXXor123
Level 1
Level 1
Posts: 7
Joined: Tue Jun 11, 2019 5:31 pm

This script will notify you if a reboot is required after automatic updates

Post by 1337haXXor123 » Tue Jun 11, 2019 6:40 pm

EDIT: post used to be about Mint 19.1, I updated it for 19.2 (but will still work in 19.1 and probably any other version)

So I use automatic updates (Update Manager > Preferences > Automation) in Linux Mint 19.2 Cinnamon and I was looking for a way to get notified if a reboot is required after installing updates. I found out that updates that require a reboot will trigger the creation of the file /var/run/reboot-required and the names of the packages in question will be stored in /var/run/reboot-required.pkgs.

So I wrote a little script that checks if the first file exists. If so, it will trigger a notification that lists the package(s) that require(s) a reboot:

Code: Select all

if [ -f /var/run/reboot-required ]; then
	export $(egrep -z DBUS_SESSION_BUS_ADDRESS /proc/$(pgrep cinnamon-sessio)/environ)
	MAINUSER=$(id -nu 1000)
	su $MAINUSER -c "notify-send -i mintupdate \"Reboot required\" \"$(cat /var/run/reboot-required.pkgs)\""&
fi
The line starting with export is needed to make notify-send work from the cron environment. For other desktop environments, try replacing cinnamon-sessio with gnome-session and maybe there's also mate-session or xfce-session? Also keep in mind the 15 character limit for process names or it won't return the process ID (that's why there is no n at the end of cinnamon-sessio).

For portability, this script assumes a regular single user installation where the main user usually has a uid of 1000. Of course you can also hardcode the username of the logged in user that needs to receive the notifications if you like/require.

The line that sends the actual notification ends in & (forks the notify-send process to the background) because otherwise the script (and I guess the anacron script that called this one too) won't finish until the notification has been closed or dismissed.

Put this script in /etc/cron.daily/ and it will check daily if a reboot is required.

I thought I'd put this out there, seeing as I searched these forums and didn't get any relevant results before I came up with this solution myself. I've used this script for a while now and it works like a charm!

notification.png
Last edited by 1337haXXor123 on Sun Aug 18, 2019 9:17 am, edited 2 times in total.

User avatar
WharfRat
Level 21
Level 21
Posts: 13080
Joined: Thu Apr 07, 2011 8:15 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by WharfRat » Tue Jun 11, 2019 11:50 pm

Nice, but the script will only work with a cinnamon desktop.

I export DBUS_SESSION_BUS_ADDRESS to $HOME/.dbus/Xdbus and include that in scripts to get a notification from a cron job.

I used it when experimenting with apparmor checking /var/crash for a file.

I like your technique though.
ImageImage

gm10
Level 18
Level 18
Posts: 8737
Joined: Thu Jun 21, 2018 5:11 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by gm10 » Wed Jun 12, 2019 10:49 am

Cool. How's your experience with the /run/reboot-required file actually getting created? I had briefly looked at it when implementing a reboot notification for the GUI part of Update Manager only, and couldn't get it created on my system at all, not even with kernel updates. I had also looked at the needrestart package, which is pretty nice, but created an unacceptable slow-down.

Btw:
1337haXXor123 wrote:
Tue Jun 11, 2019 6:40 pm

Code: Select all

	MAINUSER=$(cat /etc/passwd|grep 1000|sed "s/:.*$//g")
easier:

Code: Select all

MAINUSER=$(id -nu 1000)
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

1337haXXor123
Level 1
Level 1
Posts: 7
Joined: Tue Jun 11, 2019 5:31 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by 1337haXXor123 » Wed Jun 12, 2019 1:04 pm

WharfRat wrote:
Tue Jun 11, 2019 11:50 pm
Nice, but the script will only work with a cinnamon desktop.
Thanks, I added some info to the original post about getting it to work on other desktop environments.

@gm10:

I've seen a few notifications from this script since I implemented it about six weeks ago, so that seems to be working fine. Just now I got another one triggered by dbus.

Thanks, that looks way simpler! I encountered the MAINUSER= line somewhere else on the internet. Maybe it's the POSIX way or something? I don't know much about these standards, but you see people on Stack Overflow talk about it sometimes. Anyway, it works on Mint, so I'm going to simplify my script with this :)

So when not using automatic updates, does the GUI actually notify the user that a reboot is needed? I've only been using Mint for a few months now and turned on automatic updates pretty soon after installing it so I wouldn't know.

gm10
Level 18
Level 18
Posts: 8737
Joined: Thu Jun 21, 2018 5:11 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by gm10 » Wed Jun 12, 2019 2:42 pm

1337haXXor123 wrote:
Wed Jun 12, 2019 1:04 pm
So when not using automatic updates, does the GUI actually notify the user that a reboot is needed? I've only been using Mint for a few months now and turned on automatic updates pretty soon after installing it so I wouldn't know.
The official version does not currently. It will show an infobar after kernel updates in the GUI come LM 19.2, or with the version from my PPA (the latter also shows a desktop notification). Neither version currently checks what got installed via automatic updates or other means though, but if the /run/reboot-required file actually did get created that would be trivial to add to the update service (I changed it to a systemd-service in 19.2 from the cron job it's still using in your version. I have however stopped developing for the Mint team since - feel free to fill to void :)).

I'll have to keep an eye on it (I'll probably downgrade dbus later/tomorrow and try to see what happens after the update - I'm surprised it required a reboot, btw, because systemd should be able to reload that on that fly).
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

1337haXXor123
Level 1
Level 1
Posts: 7
Joined: Tue Jun 11, 2019 5:31 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by 1337haXXor123 » Sun Aug 18, 2019 8:58 am

So as I understand it, from Mint 19.2 onwards automatic updates are handled by a service instead of a file in /etc/cron.daily. The file (mintupdate) was not removed in the upgrade process though, so I removed it myself and indeed automatic updates are still occurring.

Since the changelog mentioned
A warning is shown if a reboot is required after a kernel update
I hoped my script wouldn't be necessary anymore, but alas, I got no notification after a kernel update was installed a few days ago. /var/run/reboot-required and reboot-required.pkgs have been created though, and after putting my script back in /etc/cron.daily, simply calling it rebootreq this time, it notified me next day. I'll update my initial post with this information.

gm10
Level 18
Level 18
Posts: 8737
Joined: Thu Jun 21, 2018 5:11 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by gm10 » Sun Aug 18, 2019 9:12 am

1337haXXor123 wrote:
Sun Aug 18, 2019 8:58 am
So as I understand it, from Mint 19.2 onwards automatic updates are handled by a service instead of a file in /etc/cron.daily. The file (mintupdate) was not removed in the upgrade process though, so I removed it myself and indeed automatic updates are still occurring.
Put it on the list of yet another issue with the official version. My version removes the old cron job automatically. It also now checks /run/reboot-required by the way, I added that shortly after we talked here, so thanks for indirectly improving my app. :) And yes, my version has desktop notifications.

I still have to have the automatic update service write /run/reboot-required though for kernel updates (they do not create that file automatically), forgot about that, so this is a good reminder.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

User avatar
karlchen
Level 20
Level 20
Posts: 11224
Joined: Sat Dec 31, 2011 7:21 am
Location: Germany

Re: This script will notify you if a reboot is required after automatic updates

Post by karlchen » Sun Aug 18, 2019 9:17 am

By the way, on Mint 19.2 Update Manager itself will display a warning that rebooting is required, e.g. after installing an update kernel.
It has done so here recently, after having upgrade kernel 4.15.0-55 to 4.15.0.58.
But this thread is about non-interactive update installations, not about interactive update installations through Update Manager. Right?!
Image
Linux Mint 19.2 32-bit xfce Desktop, Total Commander 9.22a 32-bit
Linux Mint 18.1 64-bit Cinnamon Desktop, Total Commander 9.22a 64-bit
Windows? - 1 window in every room

gm10
Level 18
Level 18
Posts: 8737
Joined: Thu Jun 21, 2018 5:11 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by gm10 » Sun Aug 18, 2019 10:07 am

karlchen wrote:
Sun Aug 18, 2019 9:17 am
By the way, on Mint 19.2 Update Manager itself will display a warning that rebooting is required, e.g. after installing an update kernel.
It has done so here recently, after having upgrade kernel 4.15.0-55 to 4.15.0.58.
But this thread is about non-interactive update installations, not about interactive update installations through Update Manager. Right?!
Yes, as mentioned above, I had added it to the GUI portion only for official Mint. OP is providing a way to receive desktop notifications even from unattended upgrades. Unfortunately from what I saw, kernel upgrades do not write to the file he is checking so it won't work for kernel upgrades.

At least not in the official version. My version will do so next week, and I'll probably also add options to show notifications both when a reboot is required and when an automatic upgrade failed (I had this on my list for a long time, we've had cases where people went months without receiving updates without knowing because they had automatic upgrades enabled and never knew they were failing). The GUI part of my app does already support desktop notifications for everything by the way.

But for users of vanilla Mint the OP's script is a very useful addition.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

1337haXXor123
Level 1
Level 1
Posts: 7
Joined: Tue Jun 11, 2019 5:31 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by 1337haXXor123 » Mon Aug 19, 2019 8:52 am

@gm10:

I have no idea, but it could also be that the Mint update process didn't remove the old cron job because I manually edited the file after it was generated.

You say kernel updates do not trigger the creation of the reboot-required file? I've received notifications from my script after kernel updates were installed in the past, and right now (I still haven't rebooted :lol:), but it lists linux-base as the package that requires a reboot, whereas this package is not listed in the update manager's History of Updates screen. The history of updates however does list linux-generic, linux-image-generic and linux-headers-generic, which I think are the packages that provide and/or come with a new kernel?

linux-base is described as
Linux image base package. This package contains files and support scripts for all Linux images.
So I guess this package requesting a reboot is related to the kernel update?

I seem to remember also having gotten notifications about linux-generic in the past (meaning reboot-required.pkgs listed linux-generic), but I could be completely wrong about that. I've always checked the update history after such a notification from my script to find out that indeed the kernel had just received an update.

Or maybe we're talking about different things? I know that to apt-get, update and upgrade are two different processes... so please let me know if there's a difference between a kernel update and upgrade.

Happy to hear my feedback contributed to the improvement of your application. Maybe I should try your version sometime :)

gm10
Level 18
Level 18
Posts: 8737
Joined: Thu Jun 21, 2018 5:11 pm

Re: This script will notify you if a reboot is required after automatic updates

Post by gm10 » Mon Aug 19, 2019 10:27 am

1337haXXor123 wrote:
Mon Aug 19, 2019 8:52 am
I seem to remember also having gotten notifications about linux-generic in the past (meaning reboot-required.pkgs listed linux-generic), but I could be completely wrong about that. I've always checked the update history after such a notification from my script to find out that indeed the kernel had just received an update.
Not according to the tests I ran, no, but it may depend on certain factors, maybe some dkms modules on your system are configured to create it. But a vanilla kernel upgrade won't write that file.
1337haXXor123 wrote:
Mon Aug 19, 2019 8:52 am
Happy to hear my feedback contributed to the improvement of your application. Maybe I should try your version sometime :)
Yes. Since I'm not using automatic updates it's good to get reminded about such things. I had raised an issue about this as a reminder on the official linuxmint github when I was still a member of the dev team but these days I don't look there anymore so it's easy to forget. ;) Anyway, I already added the feature but I'll only release it at the end of the week, so take your time if you want to check it out:

Image

It's implemented as a systemd user service that checks after login and then every 15 minutes and if a new failure or reboot-required flag are found it pops up the notification.
Tune up your LM 19.x: ppa:gm10/linuxmint-tools

Post Reply

Return to “Scripts & Bash”