Skip to content

Instantly share code, notes, and snippets.

@viviag
Last active January 7, 2020 15:25
Show Gist options
  • Save viviag/9d483fe37884461af1894d06a5e659e9 to your computer and use it in GitHub Desktop.
Save viviag/9d483fe37884461af1894d06a5e659e9 to your computer and use it in GitHub Desktop.
Update system timezone based on IP geolocation, established under Linux Munt 18.3.
#!/bin/bash
# crontab:
# */10 * * * * tzupdate.sh
# @reboot doesn't fit because of network connection delay.
# Get external IP
IP=$(curl ifconfig.me)
# Request to geolocation web service (https://ipgeolocation.io/documentation/timezone-api.html).
REQ="https://api.ipgeolocation.io/timezone?apiKey=$GEOTOKEN&ip=$IP"
data=$(curl $REQ)
# Naively parse responce - under assumption of uniqueness of "timezone" field in, which accords with docs.
tz=$(echo $data | grep -Po "(?<=\"timezone\":\")[^\"]*?(?=\")")
# Set system timezone.
timedatectl set-timezone "$tz"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment