-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-update.sh
79 lines (62 loc) · 1.79 KB
/
auto-update.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Set color variables
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
magenta='\033[0;35m'
cyan='\033[0;36m'
clear='\033[0m'
# Automatic Updates and Reboot Setup
echo -e "${yellow}\nSetting up Daily Automatic Updates and Reboot${clear}"
sleep 2
echo -e "${green}\nPlease enter the time of day (24-hour format, e.g., 04:00) for daily updates:${clear}"
read -p "Enter time (HH:MM): " time_of_day
# Validate time format (basic check for HH:MM)
if [[ ! "$time_of_day" =~ ^[0-9]{2}:[0-9]{2}$ ]]; then
echo -e "${red}\nInvalid time format. Please use HH:MM format (e.g., 04:00).${clear}"
exit 1
fi
# Paths for systemd service and timer files
SERVICE_FILE="/etc/systemd/system/daily-update-clean-reboot.service"
TIMER_FILE="/etc/systemd/system/daily-update-clean-reboot.timer"
SCRIPT_FILE="/usr/local/bin/daily-update-clean-reboot.sh"
# Create update, clean, and reboot script
cat > $SCRIPT_FILE << EOF
#!/bin/bash
# Run system updates
echo "Running apt update..."
apt update -y
apt upgrade -y
# Remove unnecessary packages and clean up
echo "Running apt autoremove..."
apt autoremove -y
apt clean
# Reboot the system
echo "Rebooting system..."
reboot
EOF
chmod +x $SCRIPT_FILE
# Create systemd service file
cat > $SERVICE_FILE << EOF
[Unit]
Description=Daily Update, Autoremove, Clean, and Reboot
[Service]
Type=oneshot
ExecStart=$SCRIPT_FILE
EOF
# Create systemd timer file
cat > $TIMER_FILE << EOF
[Unit]
Description=Run daily update, autoremove, clean, and reboot
[Timer]
OnCalendar=$time_of_day
Persistent=true
[Install]
WantedBy=timers.target
EOF
# Enable and start systemd timer
systemctl daemon-reload
systemctl enable daily-update-clean-reboot.timer
systemctl start daily-update-clean-reboot.timer
echo -e "${cyan}\nSystemd timer enabled and started successfully.${clear}"