How to find listening tcp processes
Sometimes you need to know what services are listening to what ports on your unix box. Two utilities that can get you some of this port and process information are netstat and lsof.
With netstat, you can find out which services are listening under which ports:
# netstat -na
The n option prevents ip to hostname lookups.
The a option adds to the output the sockets used by server processes.
Part of the output of the above netstat command will look like this (foreign ips have been x'd out to protect the innocent) :
$ netstat -na
Active Internet connections (including servers)
bash floating point arithmetic
Bash supports arithmetic but it does not support floating point arithmetic.
For example
bash$ echo $((1 + 1))
2
bash $ echo $((1 + 1.1))
-bash: 1 + 1.1: syntax error in expression (error token is ".1")
Bash will treat the floating point number as a string.
However, the utility bc does handle floating point arithmetic and can
be called from within bash scripts.
Here we call it from the command line:
bash$ echo "1+1" | bc
2
bash$ echo "1+1.1" | bc
2.1
Delete a file whose name begins with a dash
When using a unix terminal, sometimes you make typos and end up creating files starting with a dash "-"
For example, instead of typing
$touch ./h-ello.txt
if you ended up typing
$touch ./-hello.txt
this would create a file named -hello.txt
Trying to delete a file with a name starting with a dash will cause an error in the bash shell:
$rm -hello.txt
rm: invalid option -- h
Try `rm --help' for more information.
To get around the dash in the filename, use a double-dash "--" before the rm arguments to delete the file starting with a dash.
Using tail and awk to view part of a logfile
Logfiles can often contain information that is not immediately helpful while diagnosing a problem. For example, we may be only interested in finding what files are being currently requested via the apache log files. A typical log line would look like this:
1.2.3.4 - - [16/May/2007:07:49:50 -0700] "GET /index.php?main_page=product_info&manufacturers_id=6481&products_id=107321 HTTP/1.1" 200 5614 "-" "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)" "-"
Counting the elements in this line separated by spaces, we identify that the elements of the log line are:
fixing stale jdbc dbcp connections with testOnBorrow and validationQuery
I've recently had problems with a webapp running in tomcat trying to use a stale jdbc connection via the apache commons database connection pools library (dbcp). I fixed this problem by adding a validation query to the configuration in the webapp's context.xml file. The documentation
for dbcp options is here:
http://jakarta.apache.org/commons/dbcp/configuration.html
The change involves adding the testOnBorrow parameter explicitely to the
configuration (although by default this is set to true). However, for the testOnBorrow setting to work, you have to specify a select sql query for dbcp to validate against the database before the connection is handed to the user code. In my case, I reused the basic select from the dbcp examples 'select 1' - use the validationQuery parameter to specify the sql query.
a href usage
The link html tag
Basic usage
<a href="http://www.crumpeta.com">tech hints</a>
Link titles
The text in the link title will appear when the mouse is placed on top of the link. link title example link
<a href="http://www.crumpeta.com" title="Tech hints and notes">tech hints</a>
Page loading options
open up page in the entire window
Use this link option to bust out of frames.
full browser window example link
Including data in a perl script
Sometimes you want to include data in a perl script and not want to include the data within the code itself. With perl, you can do this by using the __END__ label and reading the data in via the DATA filehandle. For example:
#!/usr/bin/perl
while ($line = <DATA>)
{
chomp $line;
push (@whoishere, $line);
}
for ($i = 0; $i <= $#whoishere; $i++)
{
print "$whoishere[$i] is at line #$i\n";
}
__END__
you
me
us
them
will print out:
you is at line #0
me is at line #1
us is at line #2
them is at line #3
Creating DSA key for remote SSH login
This is a simple and bare bones way to create a dsa key to connect from one server to another via ssh without using passwords. In plain english: How to connect from your unix (most likely mac os x) computer to your server without typing in a password.
Say you want to connect from your computer, mason, to a remote server, dixon, running ssh.
In your home directory in mason, you need to create a DSA key:
$ssh-keygen -t dsa
Enter file in which to save the key (/home/alan/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
starting and stopping services with daemontools svc command
How to start a service with deamontools' svc command.
I am assuming that the usual /service directory exists and
all commands below are issued there!
To start up qmail-smtpd for example:
$cd /service
$svc -u qmail-smtpd
To check the service was brought up sucessfuly issue:
$svstat qmail-smtpd
qmail-smtpd: up (pid 1073) 6568 seconds
To stop a service, issue:
$svc -d qmail-smtpd
check if it is indeed down:
$svstat qmail-smtpd
I like to check all my services at once with a wildcard:
$svstat *
imaps: up (pid 1071) 6603 seconds
Fixing delete and backspace mappings in a terminal
There are a lot of solutions for this problem. But for me, the first thing to try is to change the terminal type in bash.
If you get the following and you are using bash:
$env | grep TERM
TERM=xterm-color
Try changing the .bashrc file by adding the following line:
TERM=xterm
Log out of the terminal session and log back in to use the new terminal type. Your delete key should now work properly.
This has solved my backspace and delete mismappings 90% of the time.

