Update, clean and reboot in CentOS 8

Write a shell

/usr/local/bin/centos_update_clean_reboot.sh
#!/bin/bash

# Update the system
sudo dnf -y update

# Remove old kernels
sudo dnf remove --oldinstallonly --setopt installonly_limit=2 kernel -y

# Cleanup packages
sudo package-cleanup --cleandupes -y
sudo dnf -y autoremove

# Cleanup dnf cache
sudo dnf -y clean all

# Remove tmp files
sudo rm -rf /var/tmp/*
sudo rm -rf /tmp/*

# Remove log files older than 30 days
sudo journalctl --vacuum-time=30d
sudo find /var/log/ -type f -mtime +30 -exec rm -f {} \;

echo "System update and cleanup complete, rebooting in 60 seconds..."
sudo sleep 60
sudo reboot

Grant execution right to the shell

chmod 755 /usr/local/bin/centos_update_clean_reboot.sh

Schedule a cron job to execute the shell

crontab -e
0 3 * * * /usr/local/bin/centos_update_clean_reboot.sh
systemctl reload crond.service
systemctl restart crond

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Back to Top