≡ Menu

Uninstall Perl Module

Here's how to cleanly uninstall any Perl module:

#!/usr/local/bin/perl

use ExtUtils::Packlist;
use ExtUtils::Installed;

$ARGV[0] or die "Usage: $0 Module::Namen";

my $mod = $ARGV[0];

my $inst = ExtUtils::Installed->new();

foreach my $item (sort($inst->files($mod))) {
         print "removing $itemn";
         unlink $item;
}

my $packfile = $inst->packlist($mod)->packlist_file();
print "removing $packfilen";
unlink $packfile;
{ 0 comments }

Find Out the Top 10 CPU Hogs

I found this command useful in finding out which top 10 processes are hogging my CPU resources. Note that this command is specific to the Red Hat flavor of Linux. See the man page for ps for the correct output format to use for your specific platform:

ps -eo pcpu,pid,user,args | sort -k1 -r | head -11

Substitue pcpu above with pmem to see the memory hogs instead.

{ 0 comments }

Bourne Shell Logging Routine

Here’s another logging routine, this one is written in Bourne shell:

#!/bin/sh

log_message() {
    echo `date "+%m/%d/%y %H:%M:%S %Z"` "$1" | tee -a aaa.out
}

log_message "Hello there"
log_message "Goodbye"

Sample output:

08/28/07 23:16:13 EDT Hello there
08/28/07 23:16:13 EDT Goodbye

{ 0 comments }

Setting MySQL Password

To set up root password the first time:

$ mysqladmin -u root password NeWPasSwORd

To change an existing root password:

$ mysqladmin -u root -p oldpassword newpass
Enter password:

To change a normal user password:

$ mysqladmin -u dbuser-p oldpassword newpass
{ 1 comment }

Sorting IP Addresses

The following will sort an array of IP addresses in @in and the sorted IP addresses will be in @out.

@out = sort {
    pack('C4' => $a =~ /(d+).(d+).(d+).(d+)/)
    cmp
    pack('C4' => $b =~ /(d+).(d+).(d+).(d+)/)
} @in;

What this does is it forms a string of four bytes out the IP address octet using the pack() function then sorts it lexicographically.

See also Sorting Section Numbers

{ 0 comments }

Determining the External IP Address

Here’s how to find out your external IP address courtesy of this hint:

http://www.macosxhints.com/article.php?story=20060602180942480


curl --silent http://checkip.dyndns.org
    | awk '{print $6}' | cut -f 1 -d "<"

If you are using Apple’s Airport Extreme Basestation (mine is particularly the Time Capsule and this is where I have tested this), and you have the SNMP interface enabled, you can run the following command


prompt$ snmpwalk -Os -c public -v 1 192.168.63.1 ipAdEntAddr IpAddress 
    | grep -E -v '(127.0.0|169.254|192.168.63.1)' 
    | cut -d : -f 2 | sed 's/ //g'
{ 0 comments }

Write a Daemon in Perl

The code below is a template for a daemon written in Perl. Use the code below as a starting point when you have to write a program that has to persist in the background to do its things and without a gui.


use POSIX qw(setsid);

chdir '/' or die "Can't chdir to /: $!";
umask 0;
open STDIN, '/dev/null'
    or die "Can't read /dev/null: $!";

#open STDOUT, '>/dev/null'
#    or die "Can't write to /dev/null: $!";

open STDERR, '>/dev/null'
    or die "Can't write to /dev/null: $!";

defined(my $pid = fork)
    or die "Can't fork: $!";

exit if $pid;

setsid or die "Can't start a new session: $!";

while(1) {
    sleep(5);
    print "Hello...n";
}

Note that one of the lines above is commented out to let the output print to the screen. Uncomment this in the final code to silence your program. For more on this code, see this tutorial: http://www.webreference.com/perl/tutorial/9/.

{ 0 comments }

Line Terminations

Line terminations for different operating systems:

unix 0x0a LF
Classic Mac 0x0d CR
Windows 0x0d 0x0a CR LF

To convert a text file with DOS line termination to UNIX line termination:


tr -d '15'  unixfile.txt

or


sed s/.$// winfile.txt > unixfile.txt

To convert a unix file to a DOS file:


sed s/$/x0d/ unixfile.txt > winfile.txt
{ 0 comments }

Run Periodic Maintenance Scripts Manually

Mac OS X has to run some maintenance scripts in the middle of the night to do some important housekeeping tasks. If you have a Mac that sleeps in the middle of the night, as you do, you need to run these manually by running this from the shell:


sudo periodic daily weekly monthly
{ 0 comments }

Screen Size vs. Viewing Distance

The following is a rule of thumb on the minimum viewing distance for a certain screen size:

Display Type Minimum Viewing Distance
16:9 HDTV 1.5x Diagonal
4:3 HDTV 1.8x Diagonal
Analog 3x Diagonal
{ 0 comments }