Copy script to /opt/autoupdate.sh
Script content:
#!/bin/bash
# Log file for update process
LOGFILE="/var/log/ubuntu_update.log"
# Function to log messages
echo_log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOGFILE"
}
# Remove any existing scheduled reboot
if [ -f /etc/cron.d/scheduled_reboot ]; then
rm -f /etc/cron.d/scheduled_reboot
echo_log "Removed existing scheduled reboot."
fi
# Start logging
echo_log "Starting server update..."
# Update the system
echo_log "Running apt update and upgrade..."
/usr/bin/apt update >> "$LOGFILE" 2>&1
DEBIAN_FRONTEND=noninteractive /usr/bin/apt -y -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" upgrade >> "$LOGFILE" 2>&1
if [ $? -ne 0 ]; then
echo_log "Error: apt update failed. Check the log for details."
exit 1
fi
echo_log "System update completed."
# Check if a reboot is required
if [ -f /var/run/reboot-required ]; then
echo_log "Reboot required. Scheduling reboot."
# Schedule reboot for 7:00 AM today
echo "0 7 $(date +"%d %m *") root /sbin/reboot" > /etc/cron.d/scheduled_reboot
echo_log "Reboot scheduled for 7:00 AM today."
else
echo_log "No reboot required. No reboot scheduled."
fi
echo_log "Update process completed."
exit 0
Assign execution rights to script.
chmod +x /opt/autoupdate.sh
schedule update
crontab -e
0 6 * * 2 /bin/bash /opt/autoupdate.sh
Update logs location: /var/log/ubuntu_update.log
To enable zabbix monitoring assign zabbix template in zabbix and modify agent config to include correct values for “ServerActive” & “Hostname” values
sample Zabbix template for monitoring updates:
zabbix_export:
version: '6.0'
groups:
- name: Templates/Custom
templates:
- template: Linux_autoupdate_monitor_ubuntu # keep this stable (technical name)
name: 'Ubuntu auto-update monitor (log-based)'
groups:
- name: Templates/Custom
items:
- name: 'Check Ubuntu Update Log'
type: ZABBIX_ACTIVE
key: 'log[/var/log/ubuntu_update.log,update,,,skip]'
trends: '0'
value_type: LOG
triggers:
- name: 'Auto-update completed for {HOST.NAME}'
expression: 'find(/Linux_autoupdate_monitor_ubuntu/log[/var/log/ubuntu_update.log,update,,,skip],1,,"completed")=1'
recovery_mode: RECOVERY_EXPRESSION
recovery_expression: 'nodata(/Linux_autoupdate_monitor_ubuntu/log[/var/log/ubuntu_update.log,update,,,skip],28800)=1'
priority: INFO
description: 'General notification about completed auto-update attempt.'
manual_close: 'YES'
dependencies:
- name: 'Auto-update failed, check update logs on {HOST.NAME}'
expression: 'find(/Linux_autoupdate_monitor_ubuntu/log[/var/log/ubuntu_update.log,update,,,skip],1,,"Error")=1'
recovery_expression: 'nodata(/Linux_autoupdate_monitor_ubuntu/log[/var/log/ubuntu_update.log,update,,,skip],86400)=1'
- name: 'Auto-update failed, check update logs on {HOST.NAME}'
expression: 'find(/Linux_autoupdate_monitor_ubuntu/log[/var/log/ubuntu_update.log,update,,,skip],1,,"Error")=1'
recovery_mode: RECOVERY_EXPRESSION
recovery_expression: 'nodata(/Linux_autoupdate_monitor_ubuntu/log[/var/log/ubuntu_update.log,update,,,skip],86400)=1'
priority: AVERAGE
description: |
Something failed during auto-update. Check logs on server.
Alarm will gone in 24hrs after popping-up, or you can manually close it if you check and fix issue on server.
manual_close: 'YES'
tags:
- tag: UpdateMonitoring
value: Logs