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: 3
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

So I use automatic updates (Update Manager Preferences > Auto-upgrade) in Linux Mint 19.1 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 a file called reboot-required in /var/run/ and the names of the packages in question will be stored in /var/run/reboot-required.pkgs.

So I wrote a little script that runs after the auto-upgrade when you add it to /etc/cron.daily/mintupdate and checks if this 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=$(cat /etc/passwd|grep 1000|sed "s/:.*$//g")
	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. Edit: 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.

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 Wed Jun 12, 2019 12:50 pm, edited 1 time in total.

User avatar
WharfRat
Level 21
Level 21
Posts: 12879
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 17
Level 17
Posts: 7074
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: 3
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 17
Level 17
Posts: 7074
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

Post Reply

Return to “Scripts & Bash”