2007-10-30

Font problems with Nvidia driver in Ubuntu

After I switched to using the Nvidia proprietary driver in Ubuntu instead of the nv driver, the font sizes on my system were all wacky (many, but not all, became too small). After some research I discovered that it was due to the DPI being set wrong in X. There is a great post here about what causes the problem and how to fix it:

nVidia versus fonts!

The short version is you need to figure out the DPI of your screen and then add the following lines to the Device section for your nvidia driver in xorg.conf:

Option "UseEdidDpi"   "false"
Option "Dpi" "92 x 92"

Replace 92 x 92 with the horizontal and vertical DPI of your display.

2007-10-07

Javascript date spinner control to roll ISO 8601 dates in a text field using arrow keys

I have a couple web applications where the user enters or edits dates in the near future or near past in a text field using ISO 8601 format (yyyy-mm-dd). I wanted a javascript function which would allow the user to quickly modify the date by a few days without having to delete an old date and type in a new one, i.e. just hit one key to increment or decrement the date by one day. Since I couldn't find anything out there to do this I wrote my own. The main function here is roll_dates() which gets called by the onkeydown event of a text field on a HMTL form.

I am a self-taught coder, a beginner with javascript, and I haven't carefully tested this, so please only use this as an example to help give you ideas for your own coding (i.e. don't blindly block and copy this into your own code and then be surprised when it doesn't work right or breaks something).


function date_object_to_iso_date(date_var)
{// Converts a date object to an ISO date (YYYY-MM-DD)
// Add 1 to month value since Javascript numbers months as 0-11
var month_var = (date_var.getMonth()+1)+"-";
// Add a leading zero if its a one digit month
if (month_var.length==2)
{month_var="0"+month_var}
// Add a leading zero if its a one digit date
var day_of_month_var = (date_var.getDate())+"";
if (day_of_month_var.length==1)
{day_of_month_var="0"+day_of_month_var}
var iso_date_var=date_var.getFullYear()+"-"+month_var+day_of_month_var;
return iso_date_var;
} // close function

function increment_date(date_var, var_amount)
{//Takes a date object and adds one day
date_var.setDate(date_var.getDate()+var_amount);
return date_var;
} // close function

function iso_date_to_date_object(iso_date_var)
{ //Converts an ISO date (2007-10-01) into a date object
var array_temp=iso_date_var.split("-");
var date_var=new Date(array_temp[0],array_temp[1]-1,array_temp[2]);
return date_var;
} // close function

function roll_dates(event_var, id_var)
{// Increments or decrements an ISO date using up or down arrow
// Designed to be called by the onkeydown event of a textfield
// i.e. onkeydown="roll_dates(event,'fieldid')"
var keynum_var = event_var.which;
// Only do rest of function if user hit up or down arrow
if (keynum_var == 38 || keynum_var == 40)
{var textfield_var = document.getElementById(id_var);
var iso_date_var = textfield_var.value;
// Use regular expression to check if ISO date was entered,
// and if not use today's date
var iso_check_regex = /20[0-3][0-9]-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])/;
if (iso_check_regex(iso_date_var))
{var date_var = iso_date_to_date_object(iso_date_var);}
else
{var date_var = new Date()}
// Increment or decrement value based on whether user hit up or down arrow.
switch (keynum_var)
{case 38:
date_var = increment_date(date_var,1);
break;
case 40:
date_var = increment_date(date_var,-1);
break;
} // close switch
iso_date_var = date_object_to_iso_date(date_var);
textfield_var.value = iso_date_var;
} // close if
} // close function

2007-09-30

Mount remote filesystem on Ubuntu Feisty using SSH

First install the ssh filesystem:
sudo aptitude update
sudo aptitude install sshfs


Create a mount point for the remote computer and make yourself owner:
sudo mkdir /media/remote_computer_name
sudo chown andy /media/remote_computer_name


Add yourself to the fuse group:
sudo adduser andy fuse

Log out and log back in so that the new group membership takes affect.

Mount the remote computer:
sshfs 10.10.10.120:/ /media/remote_computer_name/

There were additional steps listed on one HowTo that I didn't follow but it worked anyways:
http://ubuntuguide.org/wiki/Ubuntu:Feisty#How_to_mount_remote_host_folders_into_local_Ubuntu_machine_.28sshfs.29

2007-09-24

How to permanently mount a windows (samba) share on Ubuntu

These are barebones directions for mounting an NSLU2 drive on an Ubuntu box.

Install the smb file system: sudo apt-get install smbfs

Create a new directory under /media to use as a mount point: sudo mkdir /media/newdirectoryname.

Edit /etc/fstab: sudo nano /etc/fstab

Add a line for the new mount:
//10.10.10.110/DISK\0401 /media/newdirectoryname smbfs username=defaults,password=defaults,uid=username 0 0

Remount filesystem: sudo mount -a

For more detailed instructions: http://www.justlinux.com/nhf/Filesystems/Mounting_smbfs_Shares_Permanently.html

Additional Notes on how to allow users on Linux machine to read and write to a smbfs share that is mounted by adding entry in fstab:

  • The fourth field in an etc/fstab entry specifies the mount options for the drive being mounted. See the fstab man page or http://www.die.net/doc/linux/man/man5/fstab.5.html
  • The fstab man page doesn't give the mount options for every type of file system. For that you need the man page for the file systems. The man page for smbmount gives the syntax for specifying options in this fourth fstab field for smbfs mounts. See http://www.die.net/doc/linux/man/man8/smbmount.8.html
  • You can either use uid=username or gid=groupname to specify what user or group of users is the owner of the smbfs mount.
  • Here is the fstab entry that allowed linux user fred to copy files to smbfs mount on an NSLU2:
  • //10.10.10.110/DISK\0401 /media/nslu2 smbfs username=defaults,password=defaults,uid=fred 0 0
  • EDIT 2008-11-10: I believe that fmask and dmask properties discussed below have been deprecated and you should use umask in their place. I think a single umask=0000 in place of fmask and dmask should work, but I have not verified this.
  • After a lot of trial and error I figured out that its not enough to add the gid=groupname to the fstab entry. You also have to set fmask and dmask values which determine permissions for the mount. This fstab entry allows all members of group fredsfriends to read and write files on an smbfs mount:

    //10.10.10.110/DISK\0401 /media/nslu2 smbfs username=defaults,password=defaults,uid=fred,gid=fredsfriends,
    fmask=770,dmask=770 0 0
  • Apparently if you don’t set fmask and dmask then it uses the system default (umask value?) for file and directory permissions, which on my ubuntu install was read access but not write access.
  • I had a hard time finding out what values to use for fmask and dmask, but apparently 777 means all users have read & write access (which was apparently confirmed by running ls -l on the mount, which showed fred fredsfriends -rwxrwxrwx when dmask=777 and fmask=777), and a value of 770 gives read-write access to only the members of the group identified in gid=groupname (ls -l shows fred fredsfriends -rwxrwx—).
  • Another thing to note is that apparently in Linux only the owner of a file can change the timestamp, so a user that has read-write access to a smbfs mount by virtue of being a member of the group specified in gid=groupname will not be able to change timestamps on files on the mount, which means that when that user copies a file to the mount the file’s existing timestamp will not be carried over and instead it will be given the timestamp of when the copy action occurred.

2007-09-18

Ubuntu cheat sheet

BASH prompt tricks

  • To search back through the command line history for a particular term hit Ctrl-r and then type the term. Hit Ctrl-r again to move backwards to the next instance of the term.
Cron
  • To edit the current user's cron jobs: crontab -e
File operations
  • See size of current directory: du -hs
  • See size of specified directory: du -hs /mydirectory
  • See listing of subdirectories with sizes: du -h /mydirectory
  • List of directories sorted by size: du /mydirectory | sort -nr
  • Recursively delete directory and its contents: sudo rm -Rd /media/temp/mydirectory
  • Recursively delete the contents of the current dir: rm -R *
  • Recursively copy /media/source/Documents to directory Documents under current directory preserving permissions, ownership & timestamps: sudo cp -av /media/source/Documents Documents
  • Recursively copy /media/source/Documents to /media/temp/Documents preserving permissions, ownership & timestamps: sudo rsync -av /media/source/Documents/ /media/temp/Documents
    - Note that if you run this without sudo the ownership of all of the files and directories gets changed to the current user and at least some hidden files won't copy because of permissions issues.
Filesystem
  • Get information about a drive partition: sudo fdisk -l
  • Mount partition from command line: sudo mount -t ext3 /dev/sdb1 /media/mountpoint
    • Where ext3 is the filesystem type, /dev/sdb1 is the location of the partition and /media/mountpoint is the directory where the partition should be mounted
  • Filesystem mounting configuration: /etc/fstab
  • Remount filesystem after editing fstab: sudo mount -a
  • Unmount mount: sudo umount /mount_point
  • Fix smbfs mount that has stopped working:
    1. Unmount smbfs mount: sudo umount /problem_mount_point
    2. Delete mount point: sudo rmdir /problem_mount_point
    3. Recreate mount point: sudo mkdir /problem_mount_point
    4. Remount smbfs mount (assuming its in fstab): sudo mount -a
  • Mount Samba share from command line or script:
    sudo mount -t smbfs -o username=defaults,password=defaults,uid=usersname,gid=groupname,umask=0000 "//10.10.10.110/DISK 1" /media/zihuatanejo
Networking
  • Network configuration file: /etc/network/interfaces
  • Hosts file: /etc/hosts
  • Log on log: /var/log/auth.log
Permissions & Ownership
  • Give all permissions to all users: chmod a+rwx filename
  • Give all permissions recursively to all users to all files and directories under MyDirectory: sudo chmod -R a+rw /home/andy/MyDirectory/
  • Change ownership of file: sudo chown andy /home/andy/myfile
  • Change ownership of all files and sub-directories recursively: sudo chown -R andy /home/andy/MyDirectory
Repositories and Package Management
  • Basic Aptitude command line commands:
    • $sudo aptitude update | Updates the package lists
    • $sudo aptitude safe-upgrade | Upgrade as many packages as possible without removing existing packages.
    • $sudo aptitude full-upgrade | Upgrade everything, including deleting existing packages to solve dependencies
    • $sudo aptitude [ install | remove | purge ] pkg1 [pkg2] | Take actions on individual packages
    • $sudo aptitude search search terms | Search packages for terms
    • $sudo aptitude show pkg_name | Show information on the package

  • Repositories file: /etc/apt/sources.list
  • Editing Repositories from command line: https://help.ubuntu.com/community/Repositories/CommandLine
  • To install and configure predefined package collections, like LAMP server: sudo tasksel
SciTE
  • To configure fonts (among other things) edit: /usr/share/scite/SciTEGlobal.properties
  • To configure PHP syntax highlighting edit: /usr/share/scite/html.properties
Scripts
  • In order to run a BASH script you have to:
    • First make the file executable by running chmod 700 scriptname
    • Then to execute it from the current directory you have to use ./scriptname which translates to 'run scriptname from the current directory'
Services (Samba, SSH, Apache, etc)
  • As of Ubuntu 10.04 you can no longer start and stop services using /etc/init.d/apache2 stop.
  • The new method for starting and stopping services is: sudo service apache2 start
SSH
  • Good article on SSH port forwarding here:
    http://www.securityfocus.com/infocus/1816
  • To make a tunnel to a server behind a firewall:
    ssh -L 8888:10.10.10.120:80 andy@10.10.10.120 -p 1234
    Where 8888 is a port on the client machine, 10.10.10.120:80 is the port and IP address that you want traffic forwarded to from localhost:8888, and andy@10.10.10.120 -p 1234 is the username, IP address, and port of the remote machine you are making the ssh connection to.
  • Whenever you make configuration changes you have to restart to have them take effect:
    $sudo /etc/init.d/ssh restart
SSHFS
  • To mount:
    sshfs username@remotemachine:/remotedirectory localmountpoint
  • To unmount: fusermount -u localmountpoint

2007-09-16

MythTV Reference

To start MythTV backend: /etc/init.d/mythtv-backend start

To shutdown MythTV backend: sudo /etc/init.d/mythtv-backend stop

To backup the MythTV database: mysqldump -u mythtv -ppassword mythconverg -c > mythtv_backup.sql

To refresh listing data (if listings got messed up somehow): sudo mythfilldatabase --refresh-all
(note that this will not refresh today's listings!)

To refresh today's listing data: sudo mythfilldatabase --refresh-today

Schedules Direct: http://www.schedulesdirect.org/

Installing HP Laserjet 1012 on Ubuntu Feisty box with no GUI

This is only meant as a rough guide to the steps involved. I haven't checked it carefully and there are probably errors and omissions.

I first installed HPLIP using sudo apt-get install hplip. I then followed the instructions on how to get it set up found at http://hplip.sourceforge.net/ using the recommended driver for the HP LaserJet 1012 (HP-LaserJet_1012-hpijs.ppd) which I found at http://linuxprinting.org . It appeared to work, but whenever I tried to print a test page at the end of the hp-setup program it didn't work. I ended up with a couple versions of the same printer installed because I couldn't figure out how to delete printers from the non-GUI version of hp-setup (the particular box is an old laptop acting as a server which doesn't have a GUI).

I decided to move on for the moment and installed CUPS using sudo apt-get install cupsys.

Poked around at http://www.cups.org to figure out how to edit /etc/cups/cupsd.conf to allow access to the CUPS web interface at port 631 from a remote machine (since the box has no GUI) and how to configure CUPS so other machines on the network could connect to it to print. Made a bunch of changes, which I can’t remember, but eventually got it so I could access the CUPS web interface from my laptop by pointing to http://10.10.10.120:631/admin.

Using CUPS web interface I was able to delete all the unsuccessful printers I had created using hp-setup. When I deleted printers, etc. it prompted me for a username and password. I used root and my root password successfully. If you don't have a root password set up you may need to do some research on a workaround on cups.org to get that part to work.

I tried using the CUPS Add Printer wizard a few times to install the same recommended HPIJS driver for the HP LaserJet 1012, but whenever I tried to print a test page I got “Foomatic-rip failed” error. I poked around on Google and couldn't find anything helpful about this error.

Finally solved the Foomatic-rip failed error by deleting printer using CUPS web interface and then installing printer but choosing the HP LaserJet Series PCL 4/5 CUPS v1.2 instead of the hpijs 1012 specific driver.

I had installed Samba before using sudo apt-get install samba which presumably was necessary to allow me to share the printer with a windows machine (though I am not sure about that). I was able to successfully set up the printer as a network printer from a Windows machine following tips here: http://ubuntuforums.org/showthread.php?t=268245

2007-09-09

How to set hard drive power management in Ubuntu

To set Ubuntu so that a particular hard drive will power down after a certain amount of time enter the following:

sudo hdparm -S60 /dev/hda

The "hda" refers to the hard drive the command should apply to. Check your system to make sure you are referring to the right name for the drive your want (it was sda on my laptop).

The number following the S defines how long until the drive powers down as follows (from the hdparm man page):

Values from 1 to 240 specify multiples of 5 seconds, yielding timeouts from 5 seconds to 20 minutes. Values from 241 to 251 specify from 1 to 11 units of 30 minutes, yielding timeouts from 30 minutes to 5.5 hours. A value of 252 signifies a timeout of 21 minutes. A value of 253 sets a vendor-defined timeout period between 8 and 12 hours, and the value 254 is reserved. 255 is interpreted as 21 minutes plus 15 seconds. Note that some older drives may have very different interpretations of these values.


To turn on advanced power management for a hard drive:

sudo hdparm -B1 /dev/hda

According to the man page:

"Set Advanced Power Management feature, if the drive supports it. A low value [following the -B flag] means aggressive power management and a high value means better performance. A value of 255 will disable apm on the drive."


On my laptop I had to refer to /dev/sda, and a value of -B1 seemed to make the hard drive power down pretty quickly after use, while the hard drive seemed to keep running for a long time with a setting of -B5.

2007-08-06

Take some of the mousing out of Microsoft Word

One of the things that I hate most about Microsoft Word (and there are many) is the fact that so many frequently used actions have no keyboard shortcuts. While its possible to assign keyboard shortcuts to actions its easy to quickly run out of Ctrl-key or Alt-key combinations that can be remembered.

To solve this problem I decided to write some macros to make Microsoft Word operate more like good old XyWrite, which had a key that took you to a command line where you could enter short commands for every function.

The first step is to set up a hotkey to open the macro dialog. To do this click Tools - Customize - Keyboard (on bottom line of dialog), which gets you to the Customize Keyboard dialog. Here, select Tools in the Categories box, and then select ToolsMacro in the Commands box, then go to the Press New Shortcut Key box and hit Ctrl-m (or whatever hotkey you want), and then finally click the Assign button at the bottom. Now, whenever you hit Ctrl-m it will open the Macros dialog box, where you can type in any macro name and then hit enter to run the macro.

Next, record some macros for your most frequently used menu items, and assign them two (or three) letter names. Here are some of my favorites:

Sub pu()
' Paste Unformatted
' Same as Edit - Paste Special - Unformatted Text
Selection.PasteSpecial Link:=False, DataType:=wdPasteText
End Sub

Public Sub hy()
'highlight yellow
'Same as clicking on the highlight icon on the toolbar
Selection.Range.HighlightColorIndex = wdYellow
End Sub

Public Sub aa()
' Accept All tracked changes
ActiveDocument.AcceptAllRevisions
MsgBox "All tracked changes have been accepted."
End Sub

Sub uc()
'Upper Case; Same as Format - Change Case - Uppercase
Selection.Range.Case = wdUpperCase
End Sub

Once you have written the two letter macros for your commonly used menu items you can run them from the keyboard without ever touching the mouse by hitting Ctrl-m, then typing the two letter macro name, and then hitting Enter. It looks more cumbersome than it is; its really only a total of 4 keystrokes to run any command.

2007-05-17

Save energy and money by turning off your computer at night

I save a significant amount of electricity by turning my computer all the way off at night.

My desktop computer draws about 120 watts when its on but idle with the monitors off. Assuming I only need to have my computer on 10 hours a day, if I left it on all the time annual power consumption while idle would be:

(120 watts * 14 hours * 365 days)/1,000 = 613.2 kwh a year

Here in Arizona I pay around $0.10 per kilowatt hour (kwh) so that works out to $61.32 a year.

The first step was just powering down the desktop at night. However, since I do a long backup routine every night when I end my work day it was initially inconvenient to start my nightly backup routine, and then come back later to turn off my computer. So I used http://www.autohotkey.com/ (a multipurpose utility for Windows which can map hotkeys and automate tasks using a nice scripting language) to write a script to do all the tasks of my nightly backup routine and then shut down the computer.

However, when I took my trusty Kill-A-Watt to my workspace powerstrip I discovered that between my desktop, my Cisco IP Phone 7960, my HP printer, my Linksys router, my Xerox sheet-fed scanner, my Plantronics wireless headset, etc. I was pulling 30 watts even with everything powered down.

To a normal person leaving something on all the time that draws 30 watts is no big deal, but I am not that person. I figured that this 30 watt draw works out as follows, assuming that I want my workspace power on about 15 hours a day 6 days a week:

(((9 hours * 6 days) + 24 hours) * 30 watts * 52 weeks)/1,000 = 121.68 kwh or $12.12 per year

So, I hooked up power timer I had laying around to my powerstrip and set it so it turned the power on at 04:00 and off at 19:00.

2007-05-16

How to view multiple PDFs in separate windows using Acrobat 6

I have 2 monitors and work with a lot of PDF documents, and one thing that has always driven me crazy is that Adobe Acrobat 6 only lets you view PDFs within the application window, and it won't let you run multiple instances of Acrobat, so the only way to look at 2 or 3 PDFs side by side is to maximize Acrobat and then juggle and resize the document windows until you can see both.

Today I found a solution. Firefox (and probably IE) has an Acrobat plug-in, so to easily open multiple PDFs in separate windows you just right click on the PDF file in Windows Explorer (or your favorite file manager) and then choose "Open with" and then choose Firefox. That will launch a new instance of Firefox with the PDF