Monday, March 12, 2012

Setting the PC Time under Unix

Howdy.


Today, we are going to see how to setup your computer's time. There are cases where your PC's CMOS Battery is dead or not saving the data properly. So, you would either have to configure the time by going to your BIOS setup or set that inside your OS. Either ways, it's difficult to have it setup everytime  you login to your PC because it's just too much work to do or you can make a silly mistake. Or you can make your PC connect to the Webservice to provide the correct time (as per your local timezone). What if this can be automated? Well, this is what this article is about.


We'll write a simple unix shell script that would connect to some webservice address that will provide the current time given the position's co-ordinates. The following is a sample.

#!/bin/sh
wget http://www.earthtools.org/timezone/xx.xxx/yy.yyyy -O time.xml;
timetag=`cat time.xml|grep -i localtime`;
rm time.xml;
timetag=`echo $timetag|sed 's/<localtime>//'`;
currenttime=`echo $timetag|sed 's/<\/localtime>//'`;
echo Setting date to $currenttime;
sudo date -s "$currenttime";


Illustration:


a) I've taken earthools' simple Webservice which takes the position's geographics co-ordinates (lat/long) and provides a response XML which contains the Time we're looking for. The URL format is http://www.earthtools.org/timezone/lat/long. wget downloads the response of this URL and saves it as time.xml for later processing. You've to edit this command to use your location.
 This is how a sample response XML looks like:


<?xml version="1.0" encoding="ISO-8859-1" ?>
<timezone xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://www.earthtools.org/timezone-1.1.xsd">
  <version>1.1</version>
  <location>
    <latitude>xx.xxx</latitude>
    <longitude>yy.yyy</longitude>
  </location>
  <offset>z.z</offset>
  <suffix>A*</suffix>
  <localtime>12 Mar 2012 01:16:51</localtime>
  <isotime>2012-03-12 01:16:51 +0530</isotime>
  <utctime>2012-03-11 19:46:51</utctime>
  <dst>Unknown</dst>
</timezone>


b) Then, we extract the locatime tag by 'grep'ing it.

c) Once we have got the localtime, we remove the XML wget downloaded.

d) Then the next two sed commands remove the Start and End XML tags.

e) Now we got the actual time we're looking for.

f) Then we feed the value to the date command, which actually sets the time on the PC.

That's it. Done!

Notes:

a) The earthtools webservice is just an example. There are a whole lot of other services too. Please contact the author or owner of those sites before you use their services heavily. You should not be violating their Terms and Conditions.

b) The XML parsing shown here (using sed) is very primitive. Make it efficient as necessary.

c) If you can find a similar way of doing this on Windows platforms, please feel free to add them or paste the links to pages where this is already done.

Once you've the script ready, try running it manually every time you login or even better, automate it  by adding this script to your startup list!

Thanks for reading this. :-) See you in the next post!

Friday, January 6, 2012

Disabling X ScreenSaver on *Nix systems while running VLC Media Player

Hello.

Have you ever experienced an annoyance of the gnome screeensaver coming up when watching your favorite movie and then increasing the timeout of the screensaver just to watch that movie without getting disturbed again? Well, that's because VLC's disable-screensaver feature doesn't help really well under X (though it works perfectly fine under Windows). Some Windows users might have had problems under Windows too in earlier versions of VLC because the VLC config file (don't remember the path and file name) is not properly protected. You might have to edit the file manually and see if the line below exists and not commented (beginning with a #).

disable-screensaver=1

Well, coming back to linux problems, the following is a very simple workaround I found useful. Please try it out and let me know if this doesn't help either.

  1. Open up your favourite Terminal application and type 'cd /usr/bin'.
  2. 'sudo mv vlc vlc-original'. We're renaming the original vlc program to something of our choice.
  3. 'sudo vi vlc'. Again use your favourite editor to create a file and add the following:
  • #!/bin/bash
  • xscreensaver-command -exit
  • vlc-original "$1"
  • xscreensaver &
    4. 'sudo chmod 774 vlc'. This is required to make our "vlc script" executable.
    5. Now, either type 'vlc <path-to-your-media-file>' from the terminal or play the media from your     desktop environment.

Explanation:
  1. The first line in our script indicates that this is a bash executable script and the location of bash command.
  2. The second line just exits the xscreensaver-command service so that the screensaver is not active until you close the VLC Media Player window.
  3. The third line invokes the actual VLC program with the media you want to play. Yes, it supports playing only one at a time, but you can edit it such that it can handle as many media you want it to play. Note that the first argument ($1) is wrapped within double quotes to take care of file names with spaces in between.
  4. The last line is to make sure the screensaver service is running back again.
Hope this helps someone. See you in the next blog!