nickvergessen

Joas Schilling

Nextcloud Security Team-Lead, Nextcloud Talk Team-Lead and Senior Software-Engineer

Public key: 31B0 B117 C3A8 2811 6B83 345C 7076 EA97 51AA CDDA

« Back

🔔 How to get Nextcloud notifications for available system updates
March 17, 2017

Keep your server up to date and receive a notification in your Nextcloud when php, apache, etc. need an update with this little script.

  • Update October 03, 2024: Utilizing features of Nextcloud 30 and later.
  • Update April 21, 2017: Also works on Raspbian and others now.

Notifications for system updates

  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 5 * * * /path/to/file/system-notifications.sh
    

Leave comments in the Nextcloud forum.

[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