Archives

Tags

Showing articles with tag linux. Show all articles

Note to self: smbclient

List the available shares:

$ smbclient -U my_user -L samba_hostname

Choose the share you want, and connect:

$ smbclient -U my_user //samba_hostname/Share_Name

EASY! Right?

If you get "Not enough '\' characters", do not just keep adding them! You probably want to shuffle your arguments around (such as putting -L last).

To turn off those annoying prompts on an mget, just toggle it:

prompt

Constant outages with Passenger 2.2.11

After pushing a large Rails app live recently, I was constantly struggling to keep Passenger live. Roughly, every 24 hours passenger would simply hang and stop handling requests. The machine (VPS running CentOS 5, 2gb RAM, 4 CPUs) had sufficient free resources and memory. Restarting Apache would instantly fix the issue and flush a bunch of "Broken pipe" errors to the error logs.

I found that killing the passenger process with a SIGABRT signal (6) would cause the process to write a stacktrace to Apaches error log. This was mildly useful.

As it turns out, I was able to solve the problem by finding and killing a couple of mysteriously dormant Ruby processes that passenger had left running. Why these were still running I am not sure. The Passenger team claim that the issue will be fixed in Passenger 3, but in the mean time, keeping an eye on Ruby processes may be beneficial. My (moderately heavy traffic) app has been online for about 7 days without a hitch now. Hopefully it will stay up indefinately now.

It's also worthwhile to make sure Apaches MaxClients setting is at an appropriate level (max_clients * average_process_size). My Passenger seemed to hang up as soon as this was hit. Be careful, though, as it turns out that Apache will keep spawning new clients even if you are out of memory -- I found this out the hard way.

Here are some useful discussions: http://code.google.com/p/phusion-passenger/issues/detail?id=318 and http://code.google.com/p/phusion-passenger/issues/detail?id=199.

Running Linux processes as Daemons

Running an arbitrary process, such as a Python script, on your Linux box as a daemon is really quite simple if you have the right tools.

In my case, I had a simple Python socket server that needed to run permanently. Simply running it in the background (Ctrl-z) is no good as it would die the moment I logged off!

Enter daemon, a really handy tool for solving this exact problem.

Installation is easy. On Debian-based distros, you can just install it directly form aptitude. On CentOS, which uses yum, I had to install it from source. But I ran into no problems there.

Once installed, getting things running was easy:

$ daemon -r --name=myserver python /full/path/to/myserver.py

The "r" flag tells daemon to respawn itself if the service is killed. I've given it a name so I can refer to it later. And the rest of the line is the actual command I want to run.

To test if the process is still running, you can look in ps or pgrep, but the best way to do it is to simply ask daemon personally:

$ daemon -v --name=myserver --running

The "v" flag is for verbosity (of coarse). The "running" flag tell daemon that I want to know if the process specified by "name" is still running. The output you receive is simple to interpret, e.g: "myserver is running with PID: 23324".

To finally kill the process at some point in the future, you can issue the following command:

$ daemon -v --name=myserver --stop

And this one should be rather self-explanatory by now. :)

I've only grazed the surface here. Just check out the man page for an idea of how much more you can do with this. Good luck, muchachos!

ImageMagick and RMagick on Centos 5.3

Installing ImageMagick + RMagick seems to be a common source of problems. Here is my eventual solution.

I must admit, I'm not a huge fan of yum. And this latest experience has not helped my disposition. There is an ImageMagick(-devel) package in the repository, but it's very old now requires an earlier version of RMagick. I also found that it didn't work with the FileColumn Rails plugin ("Invalid image" error).

I recommend, for CentOS 5.x atleast, that you build ImageMagick from source.

0) Install all the dependencies (yum should be fine for these):

1) Grab ImageMagick and unpack it

$ wget ftp://ftp.imagemagick.org/pub/ImageMagick/ImageMagick.tar.gz
$ tar xvzf ImageMagick.tar.gz
$ cd ImageMagick-x.x.x

2) Compile and install ImageMagick. Note, these are the flags I used for my own server. Use --help to see a full list of the options.

$ ./configure --prefix=/usr --disable-static --with-modules --without-magick-plus-plus --with-quantum-depth=8
$ make
$ sudo make install

3) Play the waiting game. Or you can have a sword fight with an employee. :)

4) Install the latest version of the RMagick gem

$ sudo gem install rmagick

5) For good measure, open up IRB and see if it's installed correctly.

$ irb
> require 'RMagick'
> true

Good luck in your adventures, people.

Bash shells, curly braces, substitutions, attack!

Here is something I do all the time:

$ cp a-file-that-is-badass a-file-that-is-badass.old
...
$ cp a-file-that-is-badass.old a-file-that-is-badass

But, considering I'm a wannabe hacker, I knew there was going to be a much faster way of achieving the same thing. Enter the curly brace of doom!

Let me slip on my hack suit and try that again:

$ cp a-file-that-is-badass{,-old}
$ cp a-file-that-is-badass{-old,}

Here, we are utilising a feature in bash called brace expansion. Basically, it is telling the shell to prepend the contents of the command to the left of the first brace to each string inside the comma-delimited list within the braces. Anything remaining to the right of the braces is appending to each expanded string.

Brace expansion can take place anywhere inside your command. You can even nest them!

Another example (in the case of a collegue with horrible spelling):

$ mv lib/complex/{fisicks,physics}.py

The possibilities are endless...

An improved shell script for Svn resetting

A few weeks ago I posted a simple script for resetting an Svn repository to an earlier revision (and recording it as a new revision, so the reset is resettable!).

I decided it was totally shit, and so, I have simplified it a little bit:

  • You no longer have to pass in the URL for the central repository. The script will figure it out for you.
  • The working copy (second) argument is optional. It defaults to the current directory.

Anyway, here it is:

if [ $# = 0 ]
then
  echo "Resets an Svn working copy to an earlier revision."
  echo "svnreset <revision> [PATH...]"
else
  dir=$2
  : ${dir:="."}

  # Find repository URL using AWK.
  repo_url=`svn info $dir | awk '/^URL: (.+)$/ { print $2 }'`

  # Update working copy, merge into older revision, commit with message.
  svn update $dir
  svn merge -rHEAD:$1 $repo_url
  svn commit -m "Reverted back to r$1"
fi

exit 0

Call it like this (with an appropriate revision number): $ svnreset 1242

Freyja, available now!

My first Firefox extension, Freyja, is now available for download! See my first post for an introduction to the functionality and inspiration of this work.

Freyja is currently in an experimental state, and being my first attempt, may not be 100% stable on all operating systems (tested on Ubuntu 8.04, Fedora 8, Windows XP).

To use it properly, you will need Firefox 2.0 or above (FF3 is recommended), access to an SMTP server (Gmail is supported!) and the Python interpreter (comes bundled on most Linux distro's and Mac OS X).

MS Windows users -- the extension will work for you if you have Python installed on your machine.

Direct download: http://www.andrewbuntine.com/firefox/freyja.xpi

I would really appreciate it if someone could download it, test it out with their own email address(es), and let me know if you run into any problems. Any feedback at all is appreciated!

General usage is as follows:

  • Go to Tools > Freyja
  • Add a few URLs to the Websites list
  • Add one or more email addresses to the Email list
  • Go to Preferences and set your email information (example: Gmail password, email address, full name)
  • Browse to a disallowed URL (that you just set)
  • Look in the status bar for the "Sending email..." message!

Freyja: A firefox extension for porn addicts

A while ago I stumbled upon one of the weirdest/coolest/most original reasons for abandoning Linux I have seen in a long time. Check it out here: http://ubuntuforums.org/showthread.php?t=878991

Basically, the guy is heading back to Windows due to a massive addiction to pornography. He says there is not enough pornography-filtering software available for *nix systems.

Perhaps he is right? I'm not sure. But it sure is very original!

At one point in the post, he makes mention of an O/S tool that scans your browsing habits and threatens to email a person of your choosing. I thought to myself that such an idea is great for a Firefox Extension!

So I sat down and started hacking on an extension that works in the following way: The user (adictee) preconfigures the extension with a list of the websites he/she visits in order to "fill their need" and provides one or more appropriate email addresses (their mother, girlfriend/boyfriend, etc). From here, the extension silently pays attention to the users browsing and, in the occurence of the user browsing to a "disallowed" page, the supplied individuals are instantly emailed informing them of what their son/partner/whatever is up to!

I don't believe in censorship or content filtering, and so the pages will be completely available to the user, but the consequences could be dire (provided they use it with integrity!).

I have basically finished the extension and should have it packaged up and ready for download within the next day or so. I will give out more specific details (including full source code) at that time.

Also, I have decided to name it "Freyja"; a not-so-accidental reference to the Norse Goddess of Fertility.

Now there is no excuse, porn addicts all over the world can feel comfortable using free software!

Resetting your SVN repository to a previous revision

NOTE: See the improved version of this shell script here: http://andrewbuntine.com/articles/2008/11/26/an-improved-shell-script-for-svn-resetting

For the majority of the year, I have been using Git as my preferred version control system. Recently, however, I have been using Subversion on some older projects at work.

One thing I have noticed is that the "revert" command seems to be oddly named. It's more of a svn-merge-and-undo. Maybe it's just me?

In either case, I wrote a little shell script that will act more like the Git-equivelant by allowing you to forcfully revert back to any previous revision of your project. It looks like this:

if [ $# = 3 ]
then
  currdir=`pwd`

  cd $2
  svn update
  svn merge -rHEAD:$1 $3
  svn commit -m "Reverted back to r$1"

  cd $currdir
  exit 0
else
  echo "svnreset needs three arguments!"
  exit 1
fi

Save it as "svnreset" and call it like this (the period is intentional):

$ svnreset 200 . svn://server/project/trunk

That will reset your project to r200, provided you are currently in the repositories root directory. Note also, the changes will be committed as a new revision, so if you make a mistake, just reset the reset!

I suggest that you create a symlink to /usr/bin so you can just call it straight from the terminal whenever you need it.