đź”” How to get notifications for system updates

Original blog post: nickvergessen


notifications-system-update

  1. Create system-notifications.sh with the following content on your system, and make sure to adjust the path to your nextcloud /var/www/nextcloud/occ as well as your admin account name:
#!/usr/bin/env bash
 #
 # SPDX-FileCopyrightText: 2017-2024 Joas Schilling <coding@schilljs.com>
 # SPDX-License-Identifier: MIT
    
 ADMIN="admin"
 OCC_PATH="/var/www/nextcloud/occ"
    
 PACKAGES=$(/usr/lib/update-notifier/apt-check -p 2>&1)
 NUM_PACKAGES=$(echo "$PACKAGES" | wc -l)
    
 if [ "$PACKAGES" != "" ]; then
 	UPDATE_MESSAGE=$(echo "$PACKAGES" | sed -r ':a;N;$!ba;s/\n/, /g')
 	NOTIFICATION_ID=$($OCC_PATH notification:generate $ADMIN  \
 	    "{packages} require to be updated" --short-parameters "{\"packages\":{\"type\":\"highlight\",\"id\":\"count\",\"name\":\"$NUM_PACKAGES packages\"}}" \
 	    -l "Packages to update: {list}" --long-parameters "{\"list\":{\"type\":\"highlight\",\"id\":\"list\",\"name\":\"$UPDATE_MESSAGE\"}}" \
 	    --object-type='updates' --object-id='apt' --output-id-only)
     echo "$NOTIFICATION_ID" > apt-notification-id.txt
 elif [ -f /var/run/reboot-required ]; then
 	NOTIFICATION_ID=$($OCC_PATH notification:generate $ADMIN "System requires a reboot"
     echo "$NOTIFICATION_ID" > apt-notification-id.txt
 else
     NOTIFICATION_ID=$(cat apt-notification-id.txt)
 	$OCC_PATH notification:delete $ADMIN $NOTIFICATION_ID
 fi

2. Add the script to the cronjob crontab -u www-data -e:

0 10 * * * /path/to/file/system-notifications.sh

[Update] Raspbian and other systems

If your operating system does not support apt-check, you can also try to replace the line:

PACKAGES=$(/usr/lib/update-notifier/apt-check -p 2>&1)

with:

PACKAGES=$(apt-get -s dist-upgrade | awk '/^Inst/ { print $2 }' 2>&1)

This at least made it work on my Raspberry Pi which runs Raspbian.

Nextcloud 29 or older

Few of the options used above are only available in Nextcloud 30 and newer. So here is a version that works on older versions. It comes with the disadvantage of not replacing and removing the old notifications when a new one is triggered:

#!/usr/bin/env bash
#
# SPDX-FileCopyrightText: 2017 Joas Schilling <coding@schilljs.com>
# SPDX-License-Identifier: MIT

ADMIN="admin"
OCC_PATH="/var/www/nextcloud/occ"

PACKAGES=$(/usr/lib/update-notifier/apt-check -p 2>&1)
NUM_PACKAGES=$(echo "$PACKAGES" | wc -l)

if [ "$PACKAGES" != "" ]; then
	UPDATE_MESSAGE=$(echo "Packages to update: $PACKAGES" | sed -r ':a;N;$!ba;s/\n/, /g')
	$OCC_PATH notification:generate $ADMIN "$NUM_PACKAGES packages require to be updated" -l "$UPDATE_MESSAGE"

elif [ -f /var/run/reboot-required ]; then
	$OCC_PATH notification:generate $ADMIN "System requires a reboot"
fi
16 Likes

nice feature!
any way to make this work for debian?

SUPER cool, @nickvergessen !!!

I think it should work on debian as well?
At least that’s what the docs said.

Now it does. My mistake…
I had to install “update-notifier-common” and put "php " before “$OCC_PATH”.

Thanks!

Too bad pacman doesn’t have an equivalent to apt-check - this is really nice!

I published a little update: https://www.schilljs.com/2017/03/17/howto-get-notifications-for-system-updates.html


If your operating system does not support apt-check, you can also try to replace the line:

PACKAGES=$(/usr/lib/update-notifier/apt-check -p 2>&1)

with:

PACKAGES=$(apt-get -s dist-upgrade | awk '/^Inst/ { print $2 }' 2>&1)

This at least made it work on my Raspberry Pi which runs Raspbian.

@nickvergessen :sunglasses: cool!

Thanks a lot! :+1: I’d love to have that feature.
But :sweat_smile: i cant seem to get it working.

I installed admin-notifications,
the cron task (space at the end of the line, empty line at eof),
and changed the path and admin vars in the script.

I can see that cron was running in syslog and i can manually set a notification
(sudo -u www-data php /path/to/nextcloud/occ notification:generate admin “test”).
And i checked that apt-check works for www-data.

Nonetheless they wont show up :rage:
Any idea what i might be missing?

System is Nextcloud 12.0.3; Ubuntu 14.04; php 7.1.9

Can you run sudo -u www-data /path/to/file/system-notifications.sh manually? Or does that maybe error?

If i mark it as executable i get “not authorized” on line 15
($OCC_PATH notification:generate $ADMIN “$NUM_PACKAGES packages require to be updated” -l “$UPDATE_MESSAGE”).

But as i know it shouldnt be necessary to mark it as such?!
(due to #! )

ps.: owner of the file is www-data as i use apache
pss: copypasted the wrong line, now its right

great and many thanks!
but i had to add “php” to the OCC_PATH variable?!

“OCC_PATH=“php /var/www/nextcloud/occ””

should that be fixed or is it just related to my setup?

For reference: i think it’s possible to edit the script for arch linux using checkupdates instead of apt-check.
See https://wiki.archlinux.org/index.php/System_maintenance#Partial_upgrades_are_unsupported

was fixed ?? :thinking:

This works with debian 9:

ADMIN="admin"
OCC_PATH="php /var/www/nextcloud/occ"

#PACKAGES=$(apt list --upgradable 2>&1)
PACKAGESRAW=$(apt-get -s dist-upgrade | awk '/^Inst/ { print $2 }' 2>&1)
NUM_PACKAGES=$(echo "$PACKAGESRAW" | wc -l)
PACKAGES=$(echo "$PACKAGESRAW"|xargs)


if [ "$PACKAGES" != "" ]; then
        UPDATE_MESSAGE=$(echo "Packages to update: $PACKAGES" | sed -r ':a;N;$!ba;s/\n/, /g')
        $OCC_PATH notification:generate $ADMIN "$NUM_PACKAGES packages require to be updated" -l 
"$UPDATE_MESSAGE"
        echo $NUM_PACKAGES $UPDATE_MESSAGE

elif [ -f /var/run/reboot-required ]; then
        $OCC_PATH notification:generate $ADMIN "System requires a reboot"
fi
1 Like

No, you have to added it. I correct this issue and added check if user is the same as OCC owner. Also will create Log line with status. Works good under debian and ubuntu.

1 Like

Thenks :wink:

Still using it and installed it right now on a nc 17 cloud.

Please add to step 1 that is only needed for nc14 or older at https://www.schilljs.com/2017/03/17/howto-get-notifications-for-system-updates.html

Step 2: Make your sh file executable.

Agree wheth you :wink:

1 Like

welcome :wink: