Handling Shell Script Interrupts

A Real Screen Shot

A Real Screen Shot

If you do much shell scripting, then handling shell interrupts is something you should consider.  As a user is interacting with your script, they may decide to interrupt it by typing Ctrl-C, for example.  Typically this will interrupt your shell script execution, forcing it to exit.

Depending on what your shell script is doing, this could leave behind temporary files, or leave other files in a broken state.  It would be useful if you could trap the interrupt, and handle it safely, before exiting the script.
This can be achieved on most shells using the ‘trap’ command.  The trap command takes the following syntax:

trap [OPTIONS] [[ARG] SIGSPEC ... ]

The ARG is the command to be executed on signal delivery, while SIGSPEC is the name of the signal(s) to trap.  Options include -h for help, -l to list signal names, or -p to print all defined signal handlers.

For example, to always return a ‘user aborted’ error code, the following line in your script could be used.  Whatever value given to $exit_user_abort would be returned.

trap 'echo "`basename $0`: Ouch! User Aborted." 1>&2; exit $exit_user_abort' 1 2 15

The numbers 1, 2 and 15 at the end of this example define which interrupts we’re interested in trapping.  These numbers correspond to different kinds of interrupts.  A short list is given here, but you can use ‘trap -l’ for a complete list.

Signal Number Signal Name Explanation
0 EXIT exit command. Script has executed successfully.
1 HUP Hang Up. The user session has disconnected.
2 INT Interrupt.  Ctrl-C (or other shell interrupt signal) has been given.
3 QUIT Quit.  Ctrl-\ (or other shell quit signal) has been given.
6 ABRT Abort signal.
15 TERM Terminate.  Kill command has been issued against your script.

If your trap runs several commands, it’s possibly neater to call a shell function than list the commands in-line, as above.  For example:

trap funcname 1 2 15

funcname
# Function to handle interrupts
{
echo "`basename $0`: Ouch! User Aborted." 1>&2
exit $exit_user_abort
}

Howto: Pause a Shell Script

Computer Data Output

Computer Data Output

File this one under “Another Nifty script-let”.  The problem this time is getting a shell script to pause for user input.  What I needed was a way to make a script stop, saying “Hit a key to continue..”, wait for the user to hit a key, then continue the script execution.  Here’s how:

Continue reading

How to change date formats on Ubuntu

The Date Stamp

The Date Stamp

In order to change the system date format on Ubuntu, you need to know a little about where the date format is coming from.  Regional settings, such as date and time formats, as well as language, sort order, etc. are specified in files called locales.  A locale contains the rules specifying how dates and times are formatted, amongst other settings.

Continue reading

Home Network Hard Drives.

I decided to splash out on a Home Network Hard Drive this weekend.  It was a spur of the moment decision to go with the Iomega drive, as I didn’t spend much time researching options online, as I normally would do.

I plan on using the drive primarily for backups of my music, photo and other data collections.  Since I bought my girlfriend a digital camera, she’s also needed extra drive space for storing photos, etc.

The software that ships with the drive, in a word, sucked.  It looks and works like it was knocked out by a hungover engineer, the morning after the Office Christmas Party.  Usability sucked, though it was functional, and I had the Windows XP laptop configured to connect to the drive as a network share.  The drive also sports a web based administration tool, which is also functional though suffers from the same usability issues as the desktop client driver.

While the box notes the drives support for Linux, there is very little mention of it in the help or on the support section of the Iomega site.  When you search for Linux, you end up redirected to the Mac OSX instructions.  Come on Iomega, it’s obvious the drive is based on Samba, so your not entirely unfamiliar with Linux.  It won’t take a lot to document how to mount the drive on Linux, and include it in your documentation set.

Connecting the Ubuntu desktop to the drive involved installing smbfs support using:

sudo apt-get install smbfs

Then adding the following line to /etc/fstab and issuing a “mount -a” command as root.

//192.168.1.103/PUBLIC    /media/public    cifs    auto,uid=1000,gid=1000,umask=000,user    0    0

That is assuming you remain with defaults. As usernames and passwords can be changed through the web based administration tool, the connection string above would have to be modified in line.  The IP may differ for your setup also.

Since then, I have registered with Iomega, to recieve notifications of software updates.  I’ve updated the drives firmware, which forced an upgrade of the Windows client driver.  It still sucks, from a usability standpoint, but at least it sucks a little less.

Once mounted under either Windows or Ubuntu, use of the drive is like any other.  Though I have had some system freezes on Ubuntu when transferring large file sets, with large volumes of data.  I have yet to diagnose the cause of this issue.

How To Install GlassFish on Windows

1. Download GlassFish

Download the installation jar for GlassFish. I went with GlassFish V2, since this is the latest stable release, though I think this technique will work with any version listed on the download page.

2. Install it

The GlassFish V2 Installation instructions show how to install it once download is complete. The problem with the GlassFish installer is that it does not register as a service with Windows, so once you log out, the GlassFish instance will terminate.

3. Register as a Service.

There are a few ways of doing this. The Hard Way, or my favourite the Easy Way. Once the service is registered, GlassFish will continue to run once you log out, and can be controlled through the Services applet from Control Panel, though continued use of asadmin is recommended.

Alternatively, use Ubuntu, where you can install GlassFish as easy as:

sudo apt-get install glassfish

Ubuntu wins again :-)

Serial Port Jiggery-Pokery

How to enable and disable serial ports on Ubuntu

Today, I had to fix a bug in some software I’m working on. The details of the bug aren’t that important, apart from the fact that the bug only became apparent when the software was run on a system with one or less serial ports.

Since I’m debugging on Ubuntu, and I have two serial ports (/dev/ttyS0 and /dev/ttyS1), I needed to find a way to temporarily disable one. This is how I achieved it.

Continue reading

Java IDEs for x86_64

As I’ve mentioned before, I run Ubuntu on an AMD64 processor. I was recently surprised to see that running the new release of Eclipse (Europa) was causing me issues. Basically, Java (or the native parts of these projects) was throwing a wobbly when trying to run 32-bit code on a 64-bit JVM. I thought the goal of Java was write once, run anywhere!

It took me a while, but I managed to find a 64-bit install of Europa. You have to take the “Eclipse Classic” SDK version for Linux x86_64. This installed and ran using the default instructions provided (unzip and run).

Netbeans 6.0M9 gave me similar issues a while back. I never got around to fixing it, but I noticed yesterday that 6.0M10 has been released. This installed and ran straight away. I guess they’ve included x86_64 support in the installer now.

I got no excuses for not developing something useful now :-)

AddThis Social Bookmark Button

Restore Default Ubuntu Groups.

While this affected me on Ubuntu, I guess it’s possible that other Linux distros will also be affected. Though the incorrect use of usermod, I managed to screw up my groups configuration leaving me without sudo or su ability, which is obviously important on Ubuntu to make system configuration changes. Read on for the whole story of how I recovered, and restored correct groups again.

Continue reading