Timedatectl alternative

This commit is contained in:
Alexandre
2024-06-07 08:53:00 +02:00
committed by GitHub
parent 66be690588
commit df0c36d197

View File

@@ -1,16 +1,27 @@
#!/bin/bash #!/bin/bash
# Function to show the current timezone # Function to show the current timezone, with two alternative methods
show_timezone() { show_timezone() {
current_timezone=$(date +%Z) # Check if the /etc/timezone file exists
echo "Current timezone: $current_timezone" if [ -f /etc/timezone ]; then
timezone=$(cat /etc/timezone)
elif [ -f /etc/localtime ]; then
timezone=$(readlink /etc/localtime)
timezone=${timezone/\/usr\/share\/zoneinfo\//}
else
timezone="Cannot determine timezone."
fi
echo "$timezone"
} }
# Function to set the timezone # Function to set the timezone
set_timezone() { set_timezone() {
new_timezone="$1" new_timezone="$1"
sudo timedatectl set-timezone "$new_timezone" echo "$new_timezone" | sudo tee /etc/timezone >/dev/null
echo "Timezone set to: $new_timezone" sudo ln -sf /usr/share/zoneinfo/"$new_timezone" /etc/localtime
if [ -f /etc/environment ]; then sudo sed -i "/TZ/c\TZ=$new_timezone" /etc/environment; fi
if [ -d /var/run/s6/container_environment ]; then sudo echo "$new_timezone" > /var/run/s6/container_environment/TZ; fi
echo "$new_timezone"
} }
# Main script # Main script
@@ -18,11 +29,12 @@ case "$1" in
"set-ntp") "set-ntp")
case "$2" in case "$2" in
"false") "false")
sudo timedatectl set-ntp false sudo systemctl disable systemd-timesyncd
echo "NTP disabled" echo "NTP disabled"
;; ;;
"true") "true")
sudo timedatectl set-ntp true sudo systemctl enable systemd-timesyncd
sudo systemctl start systemd-timesyncd
echo "NTP enabled" echo "NTP enabled"
;; ;;
*) *)