How to obtain Sunrise & Sunset time for any location from Linux command line -

The objective is to use command line and bash shell script to obtain Sunrise & Sunset time information for any given location.
This is a companion discussion topic for the original entry at



Rob Robinett

This works great on my Pi, but I am distributing a script to a large number of users and requiring lynx is too intrusive for many of them. Could lynx be replaced with curl, which is part of the standard distro?


Lubos Rendek -> Rob Robinett

Please note that disqus comment ate most of the code hence this is a reason why we are transferring all comments to The comments for this article will be transferred soon.

For now the safest is to clone the git repository to get a working code.


Lubos Rendek -> Rob Robinett

Hi Rob,

thanks for the feedback. Lynx is extremely convenient in this case. However, I’ve got good results with curl/wget too. Try to replace the lynx line with this:

sun_times=$( curl -s$location | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g'  | grep -E "dp0-details-sunrise|dp0-details-sunset" | tr -d '\n' | sed 's/>/ /g' | cut -d " " -f 4,8 )`

It is not the prettiest code but it worked for me. Since we now work with the html output we can as well use wget to the exactly the same query:

sun_times=$( wget -qO-$location | sed 's/<span/\n/g' | sed 's/<\/span>/\n/g'  | grep -E "dp0-details-sunrise|dp0-details-sunset" | tr -d '\n' | sed 's/>/ /g' | cut -d " " -f 4,8 )

GNU/Linux is about choices and that is why I like it. I still see the lynx option more reliable as the curl and wget options where we rely on the html code.

PS: I will update this article to include curl and wget option shortly. However, the git repository is updated already.

Hope this helps…



Rob Robinett -> Lubos Rendek

Hi Lubos,
I appreciate why lynx is better and even more that you responded so quickly. I think a space character got lost in your posting since your first sed ‘s//\ng’ is missing a search term.
But you can simplify your code and execute only two instances of sed by assigning the parsed html to a bash array which you reference later:

sun_times=($( wget -qO-$location | sed 's/ /\n/g' | sed -n '/p0-details-sunrise\|dp0-details-sunset/s/[^>]*>//p' ))
# Extract sunrise and sunset times and convert to 24 hour format
sunrise=$(date --date="${sun_times[0]} AM" +%R)
sunset=$(date --date="${sun_times[1]} PM" +%R)




Works on my raspberry pi, thanks!

As a nice little addition I set it to run at 3am in crontab:

0 3 * * * fraser /home/fraser/bin/sunrise >/dev/null 2>&1

and added a line to schedule power led to turn on at sunrise:

# echo "echo input | sudo tee /sys/devices/platform/leds/leds/led1/trigger" | at $sunrise

You’ll need the ‘at’ command, install with apt-get --no-install-recommends to avoid about 24MB of mail servers which are recommended with ‘at’!


David Livingstone

Works great on my Raspberry Pi - thanks for sharing the script!


Mauricio Taslik (Angacom)

This does not work… ssl certificate error and many other issues


OldandGrumpy -> Mauricio Taslik (Angacom)

Not sure why you are having issues with this. The only thing requiring the cert is the https call, but that’s not something you can control. Lynx has been around for ages and this should work just fine. I did not try the file in the git repo, but the code above worked great. I did start by just testing to make sure the lynx call worked, first. Once I knew the lynx call would work (just type "lynx --dump "), then I knew the rest would work, as it’s just using local tools to process the lynx output.