art's abode

art.csoft.net

Archive for the ‘source-code’ Category

Trapping signals in Ruby

without comments

It’s pretty easy to handle signals in Ruby, which is sometimes necessary when you want your code to interact with the system running it.

Signalz Ahoy!

If you want to know which kill signals you have:

bash-3.2$ kill -l
1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
5) SIGTRAP      6) SIGABRT      7) SIGEMT       8) SIGFPE
9) SIGKILL     10) SIGBUS      11) SIGSEGV     12) SIGSYS
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGURG
17) SIGSTOP     18) SIGTSTP     19) SIGCONT     20) SIGCHLD
21) SIGTTIN     22) SIGTTOU     23) SIGIO       24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGINFO     30) SIGUSR1     31) SIGUSR2

If you want to know which kill signals Ruby recognises:

irb(main):002:0> Signal.list.keys.join ", "
=> "USR1, BUS, USR2, TERM, SEGV, KILL, EMT, EXIT, STOP, SYS, TRAP, INFO, IOT, HUP, INT, WINCH, XCPU, TTIN, CLD, TSTP, FPE, IO, TTOU, PROF, CHLD, CONT, PIPE, ABRT, VTALRM, QUIT, ILL, XFSZ, URG, ALRM"
irb(main):003:0>

Some quick-n-dirty source code:

There’s probably better ways of doing this, but here’s a stupid little example demonstrating how it could work:

bash-3.2$ cat signal.rb

$hups = 0

def quit_on_int
  puts "Quitting on interrupt signal."
  exit
end

def quit_on_quit
  puts "U sure killed me guud!"
  exit
end

def handle_hup
  $hups=$hups+1
  puts "HUP! (count: #{$hups})"
end

while true
  trap("INT") {quit_on_int}
  trap("HUP") {handle_hup}
  trap("QUIT") {quit_on_quit}
end

Example in use:

bash-3.2$ ruby signal.rb
HUP! (count: 1)
HUP! (count: 2)
HUP! (count: 3)
^CQuitting on interrupt signal.
bash-3.2$

We run the program as above, then send it three SIGHUPs (i.e., using "kill -HUP pid"). We break out the program using ^C (ctrl-c), sending an Interrupt. Note this may not work in windows.

bash-3.2$ ruby signal.rb
U sure killed me guud!
bash-3.2$

Here we run the program again, and send it a SIGQUIT ("kill -QUIT pid").

Written by art

December 7th, 2007 at 12:21 am

Doing a rootless ./configure; make; make install

without comments

Often I want to compile something for my normal user account. In these cases, it doesn’t make sense to install system-wide. Lots of source tarballs nowadays allow you to compile and install a program in a particular location.

In general, you should be doing something like so:

$ tar -jxvf some_fantastic_warez.tar.bz2

$ cd some_fantastic_warez
$ ./configure ––prefix=~/usr

$ make

$ make install

make install will place your compiled binary into the path you specified with the ––prefix option when running configure. In this case I’ve set it to install it under the directory tree ~/usr (i.e., /home/username/usr).

If the source is fairly standard, you will then find the binary as ~/usr/bin/some_fantastic_warez.

You may wish to read about modifying your $PATH, so that you don’t need to specify the full path to run your program.

Written by art

November 6th, 2007 at 11:32 pm

Posted in shell,source-code