Last active
April 3, 2025 06:19
-
-
Save Juravenator/da633eef2c48d48a22895867c1ede2c3 to your computer and use it in GitHub Desktop.
RHEL/Fedora auto-update & reboot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -o errexit -o nounset -o pipefail | |
| IFS=$'\n\t\v' | |
| if [ "$EUID" -ne 0 ]; then | |
| echo "Attempting to re-run as root..." | |
| sudo "$0" "$@" | |
| exit | |
| fi | |
| echo "✅ running as root" | |
| if ! rpm -q dnf-automatic &>/dev/null; then | |
| echo "installing dnf-automatic..." | |
| dnf install -y dnf-automatic | |
| fi | |
| echo "✅ dnf-automatic is installed" | |
| TIMER_FILE="/usr/lib/systemd/system/dnf-automatic-install.timer" | |
| sed -i -- "s|^OnCalendar=.*$|OnCalendar=*-*-* 23:00|g" "${TIMER_FILE}" | |
| sed -i -- "s|^RandomizedDelaySec=.*|RandomizedDelaySec=5m|g" "${TIMER_FILE}" | |
| systemctl daemon-reload | |
| systemctl enable dnf-automatic-install.timer | |
| echo "✅ dnf-automatic installs updates daily at 23:00" | |
| CRON_SCHEDULE="0 0 * * 4" | |
| CRON_COMMAND="/usr/sbin/shutdown now" | |
| CRON_FILE="/etc/crontab" | |
| CRON_ENTRY="${CRON_SCHEDULE} root ${CRON_COMMAND}" | |
| if ! grep -q "${CRON_COMMAND}" "$CRON_FILE"; then | |
| echo "${CRON_ENTRY}" >> "${CRON_FILE}" | |
| else | |
| sed -i -- "s|^.*${CRON_COMMAND}$|${CRON_ENTRY}|g" "/etc/crontab" | |
| fi | |
| echo "✅ system reboots every thursday at midnight" | |
| echo "🚀 done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment