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