Simple TCP Daemon Example

Using some stuff I’ve covered in the past on my blog here’s a simple way to put up a daytime server (well to put any service onto a tcp port. I haven’t looked into its bi-directional capabilities yet, this was just sort of a proof-of-concept…

$ apt-get install ipsvd
$ wget http://blog.apokalyptik.com/files/daemonize/daemonize.c
$ cc daemonize.c -o daemonize
$ ./daemonize /var/run/daytime.pid /var/log/daytime.log 'tcpsvd 0 13 date'

start/stop and/or monit script are an extremely short jump from there… And kind of trivial/menial… so I leave that as an exercise to you… if you care :)


Posted on : Jun 18 2008
Posted under Business, Random Thoughts, Software Development, cli, linux |

Is it simple enough?

One question I sometimes forget to ask myself, when I’m coming up with solutions to problems is this: “Is it simple enough?”  Even though I try and make a habit of asking myself that often I, sometimes, don’t ask often enough.    The lesson of the importance of simplicity in both software development and systems management is one of those lessons that you really only ever “learn” after you’re war scarred and battle weary.  One day it clicks, and you realize “hey… this should be easier.”

The interesting thing about the problems that we (as a community of tech minded developer types, and systems people) is that the majority of our solutions can be done easily.  Sure there’s always something… at the deep dark core of a really difficult problem… that will always be that arcane black magic voodoo bit of code.  But by and large everything else can (and should) be simple.

I wonder what stories you, my 2.5 readers, may have to share on the subject.  When was it that you became totally fed up with writing complex solutions?  Or have you some other opinion on the matter?


Posted on : May 23 2008
Posted under Random Thoughts |

AIM Spam

has any body else noticed a huge increase in AIM spam lately?

I’m just waiting for the akismet-style service/plugin combo to come along for the IM space… It’s probably long overdue.


Posted on : May 04 2008
Posted under Random Thoughts |

Daemonize Anything

I hacked together this little C program from this other little c program. Basically acts as an execution wrapper that lets you fork() and detach and run a command in the background with a pidfile and log file for program output. So far I havent had any problems with it… but then I’m not a true C guy so any input is welcomed.


Posted on : Apr 10 2008
Posted under Business, Personal, Random Thoughts, Software Development, cli, linux |

Bella and buddy, represent’n

They’re down with the Dubya P.








Posted on : Mar 31 2008
Posted under Bella, Business, Personal, Random Thoughts, buddy, dogs, funny stuff |

Autumn Leaves Leaf (Irc Bot) — Chanlogger

Simple, straight forward. Logs channel messages.

class Chanlogger < AutumnLeaf
 def did_receive_channel_message(sender, channel, msg)
  log_base = File::dirname(File::dirname(__FILE__)) + "/data/logs"
  subdir = log_base + "/" + Date::today.to_s[0..6]
  Dir::mkdir(subdir) if false == File::directory?(subdir)
  logfile = subdir + “/” + channel.to_s.sub(’#',”) + “@” + Date::today.to_s
  f = File::open(logfile, ‘a’)
  f.puts(Time::now.to_s + “\t” + sender.to_s + “\t” + msg.to_s)
  f.close()
 end
end

Posted on : Jan 16 2008
Posted under Random Thoughts |

Just a friendly new years eve reminder!

Keep your WP installs up to date!  You never know who might b using the holidays (and their general hustle and bustle) to cloak their activities. Often times you’ll find that bad things happen while you we’re off doing other things for a while. This is usually no coincidence :)

So upgrade those WP’s kiddies!


Posted on : Dec 31 2007
Posted under Random Thoughts |

Autumn Leaves Leaf #2: Feeder

This handy little bot keeps track of RSS feeds, and announces in the channel when one is updated. (note: be sure to edit the path to the datafiles) Each poller runs inside its own ruby thread, and can be run on its own independent schedule

require 'thread'
require 'rss/1.0'
require 'rss/2.0'
require 'open-uri'
require 'fileutils'
require 'digest/md5'

class Feeder < AutumnLeaf

def watch_feed(url, title, sleepfor=300)
  message "Watching (#{title}) [#{url}] every #{sleepfor} seconds”
  feedid = Digest::MD5.hexdigest(title)
  Thread.new {
    while true
      begin
        content = “”
        open(url) { |s|
          content = s.read
        }
        rss = RSS::Parser.parse(content, false)
        rss.items.each { |entry|
          digest = Digest::MD5.hexdigest(entry.title)
          if !File.exist?(”/tmp/.rss.#{feedid}.#{digest}”)
            FileUtils.touch(”/tmp/.rss.#{feedid}.#{digest}”)
            message “#{entry.title} (#{title}) #{entry.link}”
          end
          sleep(2)
        }
      rescue
        sleep(2)
      end
      sleep(sleepfor)
    end
  }
  sleep(1)
end

def did_start_up
  watch_feed(”http://planet.wordpress.org/rss20.xml”, “planet”, 600)
  watch_feed(”http://wordpress.com/feed/”, “wpcom”, 300)
end

end

Posted on : Oct 02 2007
Posted under Business, Random Thoughts, Software Development, blogging, cli, linux, ruby on or off rails |

Autumn Leaves Leaf #1: Announcer

This bot is perfect for anything where you need to easily build IRC channel notifications into an existing process. It’s simple, clean, and agnostic. Quite simply you connect to a TCP port, give it one line, the port closes, the line given shows up in the channel. eg: echo ‘hello’ | nc -q 1 bothost 22122

require 'socket'
require 'thread'

class Announcer < AutumnLeaf

        def handle_incoming(sock)
                Thread.new {
                line = sock.gets
                message line
                sock.close
                }
        end

        def did_start_up
                Thread.new {
                        listener = TCPServer.new('',22122)
                        while (new_socket = listener.accept)
                                handle_incoming(new_socket)
                        end
                }
        end

end

Bash: “I Can’t Eat Another Byte”

root@server:/dir/ # ls | wc -l

1060731

root@server:/dir/ # for i in *; do rm -v $i done; done

me@home:~/ #

HUH?

Turns out that bash just couldn’t eat another byte, and next time I logged in I saw this: “bash[5469]: segfault at 0000007fbf7ffff8 rip 00000000004749bf rsp 0000007fbf7fffe0 error 6“… Impressive :)


Posted on : Sep 13 2007
Posted under Random Thoughts, cli, funny stuff, linux |