Tom Duckering

Icon

a web log of my technical stumblings

NetCat, FIFOs for Fun with Servers over TCP

I wanted to interact with a beanstalkd queue server to delete all the items in a queue (tube in beanstalk language). It didn’t have a command to drop an entire queue, but fortunately the interface is just ASCII over TCP. I know that I can use netcat and some scripting but it’s not totally straight forward since need to maintain a connection since it held state about my current queue.

The following code represents a script which is echoing commands to the server and reading it’s responses. It uses echo -e so that I can add a line terminator \r that the server expects. I have to read the response immediately (for reasons that I shall explain later) using read. I issue a couple of setup commands to make sure I’m using the right queue and not picking items from the default queue. Then I enter a loop to issue a reserve command and then fetch the job id and use that to issue a delete command with that id. I repeat until I get a response from the server which indicates that I’ve not been able to reserve a new job.

It’s all a bit tricky because Unix allows you to easily connect one process to another when one process’s output is to be the input for another process. It gets a bit harder when you want to connect them in both directions. To do this you can use a named pipe (aka a FIFO) – a basic filesystem object for allowing simple interprocess communication. You can make them very easily like so:

mkfifo myfifo

To have the script work with netcat you do this:

./delete_queue.sh < myfifo | nc localhost 11300 > myfifo

This allows the delete queue script to talk to the server via an anonymous pipe and receive the response via the named pipe (myfifo).

delete_queue.sh source:
echo -e "use myQueue\r"
read LINE
echo -e "watch myQueue\r"
read LINE
echo -e "ignore default\r"
read LINE

while true
do
echo -e "reserve\r"
read RESERVED_RESPONSE_1

if [[ $RESERVED_RESPONSE_1 == RESERVED* ]]
then
JOB_ID=$(echo $RESERVED_RESPONSE_1 | cut -d" " -f2)
echo "Job ID is ${JOB_ID}" 1>&2
read RESERVED_RESPONSE_2
echo -e "delete ${JOB_ID}\r"
read DELETE_RESPONSE
else
exit 0
fi
done

Filed under: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: