Bash - How to retrieve sunrise and sunset times


In this page I provide the script code for some Weather services in order to retrieve sunrise and sunset times, eventually using them to set one or two cron scripts. Please take note about the following points:

  • All code in this page relied on the HTML code of the Weather services at the time I've inserted on this page. As soon as the Weather service changes the HTML code inside its own pages, the script code on this page for that Weather service is no more reliable.
  • Script must be executed at least once a week, so schedule it according to your targets. I suggest you run it every day in the middle between sunrise and sunset times.
  • This code has been tested on router Asus RT-AC86U using an Entware installation. Entware installation has been used only due to the "date" command, as the "date" command provided by the router does not support some features. If you note that the "date" command of your Linux environment properly works, so you don't need to install Entware. Please refer to this page on how to install Entware on a router.
  • At the end of each script, there are some "cru" commands included in a comment. Command "cru" is used by router Asus RT-AC86U in order to add a line to the "cron" service. These two additional rows allow to run one script at sunrise time and another one at sunset time.
  • I've used the Italian "Milan" city as location example in each script. Please follow the instructions inside each script in order to properly retrieve the sunrise/sunset times for your location.

www.datameteo.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.datameteo.com
#
# 1) go to address http://www.datameteo.com/meteo/
# 2) insert your location inside field "Search a place"
# 3) copy and paste the internet address here in this script
#

url="http://www.datameteo.com/meteo/weather_Milan"

# we get all the web page
allfile=$( wget -qO-  $url | sed 's/<br/\n/g' | sed 's/<\/span>/\n/g' | grep -i ">Sunrise");

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | cut -d ":" -f 2,3 | sed 's|[^0-9]*\([0-9\:]*\)|\1 |g')
sunrise_str=$(echo $allfile | cut -d ":" -f 4,5 | sed 's|[^0-9]*\([0-9\:]*\)|\1 |g')
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.eurometeo.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.eurometeo.com
#
# 1) go to address http://www.eurometeo.com/english/search
# 2) insert your location
# 3) copy and paste the internet address here in this script
#

url="http://www.eurometeo.com/english/forecast/city_LIML/weather/milan.htm"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<br/\n/g' | sed 's/<\/br>/\n/g' | grep -i "The sun rises" | cut -d ">" -f 5 | cut -d "<" -f 1)
sunrise_str=$(echo $allfile | sed 's/<br/\n/g' | sed 's/<\/br>/\n/g' | grep -i "The sun rises" | cut -d ">" -f 7 | cut -d "<" -f 1)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.ilmeteo.it

#!/bin/bash

# Only for italian users
#
# We retrieve sunrise and sunset times from ilmeteo.it
#
# 1) go to address https://www.ilmeteo.it
# 2) insert your location
# 3) copy and paste the internet address here in this script
#

url="https://www.ilmeteo.it/meteo/Milano"

# we get all the web page
allfile=$( wget -qO-  $url | sed 's/<script/\n/g' | sed 's/<\/script>/\n/g'  | grep -E "bi-eff-sole" | tr -d '\n');

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | cut -d " " -f 3 | cut -d "," -f 1)
sunrise_str=$(echo $allfile | cut -d " " -f 5 | cut -d "'" -f 1)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

en.sat24.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from en.sat24.com
#
# 1) go to address https://en.sat24.com
# 2) insert your location
# 3) select tab "Today"
# 4) copy and paste the internet address here in this script
#

url="https://en.sat24.com/en/forecast/t/2876761/milano"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g' | grep -i "sunrise" | cut -d ">" -f 2)
sunrise_str=$(echo $allfile | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g' | grep -i "sunset" | cut -d ">" -f 2)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.timeanddate.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.timeanddate.com
#
# 1) go to address https://www.timeanddate.com/
# 2) insert your location inside field "World Clock"
# 3) go to field "Sun & Moon" and select "Sun & Moon Today"
# 4) copy and paste the internet address here in this script
#

url="https://www.timeanddate.com/astronomy/italy/milan"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<p/\n/g' | grep -i "Sunrise Today" | cut -d ">" -f 5 | cut -d "<" -f 1 | tr "." :)
sunrise_str=$(echo $allfile | sed 's/<p/\n/g' | grep -i "Sunset Today" | cut -d ">" -f 12 | cut -d "<" -f 1 | tr "." :)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

weather.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from weather.com
#
# 1) go to address https://weather.codes/search/
# 2) insert your location
# 3) copy and paste the output location code here in this script
#

location="ITXX0042"

# we get all the web page
allfile=$( wget -qO-  https://weather.com/weather/today/l/$location);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g' | grep -E "dp0-details-sunset" | cut -d ">" -f 2)
sunrise_str=$(echo $allfile | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g' | grep -E "dp0-details-sunrise" | cut -d ">" -f 2)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.weather-forecast.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.weather-forecast.com
#
# 1) go to address https://www.weather-forecast.com
# 2) insert your location
# 3) copy and paste the internet address here in this script
#

url="https://www.weather-forecast.com/locations/Milan/forecasts/latest"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile | sed 's/<img/\n/g' | sed 's/<\/span>/\n/g' | grep -E "b-forecast__table-value" | grep -i "sunrise" | cut -d ">" -f 7)
sunrise_str=$(echo $allfile | sed 's/<img/\n/g' | sed 's/<\/tr>/\n/g' | grep -E "b-forecast__table-value" | grep -i "sunset" | cut -d ">" -f 11 | cut -d "<" -f 1)
echo $sunset_str
echo $sunrise_str

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_str" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_str pm" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#

 

www.wunderground.com

#!/bin/bash

#
# We retrieve sunrise and sunset times from www.wunderground.com
#
# 1) go to address https://www.wunderground.com/
# 2) insert your location
# 3) press tab "HOURLY"
# 3) copy and paste the internet address here in this script
#

url="https://www.wunderground.com/hourly/it/milan?cm_ven=localwx_hour"

# we get all the web page
allfile=$( wget -qO-  $url);

# we get the original sunrise/sunset time values
sunset_str=$(echo $allfile |  tr -d '\n' | sed 's/<div/\n/g' | sed 's/<\/div>/\n/g'  | grep -E "astro-data sunrise-icon")
sunrise_str=$(echo $allfile |  tr -d '\n' | sed 's/<div/\n/g' | sed 's/<\/div>/\n/g'  | grep -E "astro-data sunset-icon")
echo $sunset_str
echo $sunrise_str

# we get time values and am/pm values
sunset_time1=$(echo $sunset_str | cut -d ">" -f 3 | cut -d "<" -f 1)
sunset_time2=$(echo $sunset_str | cut -d ">" -f 5 | cut -d "<" -f 1)
sunrise_time1=$(echo $sunrise_str | cut -d ">" -f 3 | cut -d "<" -f 1)
sunrise_time2=$(echo $sunrise_str | cut -d ">" -f 5 | cut -d "<" -f 1)

# we convert time values to Time in 24-hour format
sunset=$(/opt/bin/date --date="$sunset_time1 $sunset_time2" +%R)
sunrise=$(/opt/bin/date --date="$sunrise_time1 $sunrise_time2" +%R)
echo $sunset
echo $sunrise

# we get hour/min values for sunset
sun_set1=$(echo $sunset | cut -d ":" -f 1)
sun_set2=$(echo $sunset | cut -d ":" -f 2)
echo $sun_set1
echo $sun_set2

# we get hour/min values for sunrise
sunrise1=$(echo $sunrise | cut -d ":" -f 1)
sunrise2=$(echo $sunrise | cut -d ":" -f 2)
echo $sunrise1
echo $sunrise2

# uncomment if you want to delete two cron rows for sunset and sunrise scripts
#cru d sunsetscript
#cru d sunrisescript

# uncomment if you want to add two cron rows for sunset and sunrise scripts
#cru a sunsetscript  "$sun_set2 $sun_set1 * * * sh /opt/etc/myscripts/sunsetscript.sh"
#cru a sunrisescript "$sunrise2 $sunrise1 * * * sh /opt/etc/myscripts/sunrisescript.sh"

# crontab values
#
# .---------------- [m]inute: (0 - 59)
# |  .------------- [h]our: (0 - 23)
# |  |  .---------- [d]ay of month: (1 - 31)
# |  |  |  .------- [mon]th: (1 - 12) OR jan,feb,mar,apr...
# |  |  |  |  .---- [w]eek day: (0 - 6) (sunday=0 or 7)  OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
#
# *  *  *  *  *  command to be executed
#