-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalpine-auto-update.sh
More file actions
59 lines (45 loc) · 1.55 KB
/
alpine-auto-update.sh
File metadata and controls
59 lines (45 loc) · 1.55 KB
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
#!/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
# Path for update, clean, and reboot script
SCRIPT_FILE="/usr/local/bin/daily-update-clean-reboot.sh"
# Create update, clean, and reboot script
cat > $SCRIPT_FILE << EOF
#!/bin/sh
# Run system updates
echo "Running apk update..."
apk update
apk upgrade
# Remove unnecessary packages and clean up
echo "Running apk autoremove..."
apk del \$(apk info -v | awk '/^i/ {print \$1}')
# Reboot the system
echo "Rebooting system..."
reboot
EOF
chmod +x $SCRIPT_FILE
# Set up the cron job
echo -e "${yellow}\nSetting up cron job for daily updates at $time_of_day${clear}"
# Add cron entry for daily updates at the specified time
echo "$time_of_day * * * * root $SCRIPT_FILE" >> /etc/crontab
# Start cron service if it's not running
echo -e "${cyan}\nStarting cron service...${clear}"
/etc/init.d/crond start
rc-update add crond
echo -e "${cyan}\nCron job set up successfully to run daily updates at $time_of_day and reboot the system.${clear}"