Skip to content

Instantly share code, notes, and snippets.

@GongT
Created August 13, 2023 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GongT/bbebff6e4c396da3f305abcd40f80e71 to your computer and use it in GitHub Desktop.
Save GongT/bbebff6e4c396da3f305abcd40f80e71 to your computer and use it in GitHub Desktop.
archlinux pacman auto update
########### /usr/local/bin/auto-update
#!/usr/bin/env bash
set -Eeuo pipefail
declare -i RETRY=3
while ! pacman -Syu --noconfirm; do
RETRY=$((RETRY - 1))
if [[ $RETRY -le 0 ]]; then
echo "still not update after 3 tries, maybe network issue." >&2
exit 1
fi
echo "update failed, retry $RETRY..."
done
CURRENT_KERNEL=$(uname -r)
LATEST_KERNEL=$(pacman -Q linux | cut -d' ' -f2)
simple_check() {
if bootctl status --no-pager | grep -F 'No such file'; then
echo "failed check kernel version, bootctl report missing files, wall to everyone..." >&2
wall 'Something wrong when updating kernel, please check it manually. (journalctl -b -u auto-update.service)'
exit 1
fi
}
echo "update success!" >&2
if [[ $CURRENT_KERNEL != "$LATEST_KERNEL" ]]; then
echo "current kernel: $CURRENT_KERNEL, latest kernel: $LATEST_KERNEL, rebooting..." >&2
simple_check
if [[ $(who | grep -vP '\btty\d+\b' | wc -w) -ge 1 ]]; then
echo "some user login, alert and schedule reboot in 5 minutes" >&2
wall '系统更新了内核,5分钟后自动重启,运行 `systemctl reboot --when=cancel` 取消此操作。
Kernel updated, system will reboot in 5 minutes, run `systemctl reboot --when=cancel` to cancel.'
systemctl reboot --no-wall --when=+5min
else
echo "all clear, reboot now!!!" >&2
systemctl reboot
fi
else
echo "kernel version has no change" >&2
fi
########### /etc/systemd/system/auto-update.service
[Unit]
Description=系统自动更新
Requires=multi-user.target
After=multi-user.target
[Service]
Type=oneshot
RemainAfterExit=no
ExecStart=/bin/bash /usr/local/bin/auto-update
Restart=no
TimeoutSec=30min
EnvironmentFile=/etc/profile.d/50-environment.sh
########### /etc/systemd/system/auto-update.timer
[Unit]
Description=系统自动更新计时器
Requires=multi-user.target
After=multi-user.target
[Install]
WantedBy=timers.target
[Timer]
OnCalendar=*-*-* 03:00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment