Kill a linux process by name

The kill command is used to kill a process using its pid, but there are times when you need to kill the process by its name.

killall “command” is usually used to do this, but sometimes there are issues where it returns “No matching processes belonging to you were found”. Killall isn’t the greatest at finding the correct process name (especially when your process is being used by an interpreter).

NOTE: Keep in mind that “ps ax” and “ps aux” return two different command names, a long form and short form respectively.

I ran into this issue, so I wrote the following to find the process I needed to kill much easier.

kill -15 `ps ax | grep “[0-9] <command>” | awk -F” ” ‘{print $1}’`

NOTE: You must replace <command> with the command you like.

This gets the specific pid for your chosen command and passes it to the kill application.

Specifically, I needed to kill a nodejs script that was being interpreted by the node interpreter.

This had to be done in a crontab every X minutes. Killall wasn’t finding the process on Ubuntu, but it seemed to be working on OSX so I wrote this method that worked on both OSs.

kill -15 `ps ax | grep “[0-9] node bin/server.js” | awk -F” ” ‘{print $1}’`

Transparent Facebook XFBML “Like” button

Problem:

You want to add a Facebook “Like” button to your website via XFBML, but you use non-white background. Maybe a particular colour, or an image that repeats itself.
By default, the “Like” button is rendered in an iframe with a white background.

Solution:

Use CSS to make the iframe’s background transparent.

Code:

XFBML “Like” button.
NOTE: I enclosed the XFBML in my own div that I will apply my custom style to.

<div class="fb-like">
    <fb:like href="http://ronnaraeleonard.ca" send="true" layout="button_count" width="90" show_faces="false"></fb:like>
</div>

CSS to set the transparency of the iframe:

.fb-like iframe {
    background: transparent;
}

Bam! Sexy-looking Facebook XFBML Like button that doesn’t break my site’s layout.

Check it out in action at http://ronnaraeleonard.ca/news

Before (note ugly white-background)

After (transparent background)

 

Virtualizing your workspace

Over the years I’ve gone through many OS updates, software upgrades, hard drive reformats, hard drive crashes… you name it. Each time, even with backups, it’s always a pain to have to reinstall all the server software, components, dependencies, etc.

Enter virtual machines.

For those of you unfamiliar, a virtual machine is exactly what the name implies: virtualized hardware (machines) that allows you to run multiple operating systems at the same time. For more information, see http://en.wikipedia.org/wiki/Virtual_machine.

Virtual machines aren’t new at all and I have used them for many of years for various tasks (testing new software, test / tracing viruses without infecting my host computer, network testing, and gaming to name a few), but previously I hadn’t used them that much for my web development work (aside from browser testing). I’ve tried both Parallels and VMWare Fusion and at the moment don’t have a preference, however, currently I use VMWare Fusion.

My setup:

  • Host OS: OSX
  • Guest OS (VM): Ubuntu 64bit
  • Web server: Lighttpd, PHP w/ FastCGI and MySQL

I set this up awhile ago and so far it has worked great.

The benefits are many:

  • Anytime I perform a host OS upgrade, I just need to copy my virtual machine to my local disk and my development server is ready for work.
  • I can copy my virtual machine to any number of computers and run the same environment regardless of whether I’m using my desktop or my laptop.
  • Virtual machines can be used regardless of the host operating system without affecting my server applications or impacting my work
  • Since I keep regular “snapshots” of my virtual machine, I can easily roll the virtual machine back to a previous state if there are issues with upgrading the guest OS… all without impacting my host OS.
  • Combining a virtual server with Git makes sure any project I need can be easily retrieved and updated, regardless of where I am.

In the end, the most valuable thing it gives me is a ton of time saved and as much redundancy as I want.

Cybersource SOP Response Handlers from Multiple Domains

I noticed quite a few people were having issues with this (including myself), and it wasn’t clearly mentioned in the documentation. If it helps you, please leave a comment!

Problem:
1. You use Cybersource as a payment processor.
2. You’re using Cybersource’s SOP option.
3. You have multiple domains that use Cybersource, but you can only specify a single URL as your response handler (a page that handles the decline/receipt response). You need to be able to send people to the appropriate domain, based on where they came from (that is, if you’re coming from Domain A you should be sent back to Domain A).

Answer: The following input fields allow you to override the response URL configured in the Test Business Center. This allows you to add your own custom response page, based on the location you’re coming from.

<input name="orderPage_receiptResponseURL" type="hidden" value="http://domain-a.com/response_handler" />
<input name="orderPage_declineResponseURL" type="hidden" value="http://domain-a.com/response_handler" />
<input name="orderPage_sendMerchantURLPost" type="hidden" value="true" />
<input name="orderPage_merchantURLPostAddress" type="hidden" value="http://domain-a.com/response_handler" />

A little more in-depth, for the curious

What is Cybersource?

Cybersource, [is] a leading provider of Credit Card Processing for Business, Electronic Payment & Risk Management Solutions also provides solutions to enable electronic payment; avoid online credit card fraud and credit card processing for Web, Call center & POS environments.

What is SOP?

SOP (Silent Order Post) is a feature that Cybersource provides that allows you to provide an online credit card form and submit the information to Cybersource for processing.

Let me give you a tiny bit of background on how the flow works (not going into any specifics):

1. User inputs information in your form
2. User submits the form… the form is sent directly to Cybersource
3. Cybersource processes the information, and sends the user back to a “response handler” (URL) the developer specified in the business center
4. The response handler displays an error message or receipt based on the response from Cybersource

Most people seem to only think that the response handler must be configured in the Business Center and I actually thought this as well but after scouring their documentation and trying various methods, I found that these fields may be customized within your form data.

For more information, feel free to check the following links.

Information about SOP

SOP User Guide (HTML format)

How to install PEAR, PHPUnit, Testing_Selenium PEAR package, and Selenium RC in Windows Vista:

What is PHPUnit?

  • PHPUnit is a testing framework to create and run automated unit tests for your web application.
  • PHPUnit helps test the back-end of your web application by running through certain parts (units) and test them, comparing them with the expected output

What is Selenium?

  • Selenium is a suite of test tools to help test the front-end of your web application. The front -end being the visual aspect, and browser compatibility.
  • Using Selenium you can create automated tests to ensure your product is less prone to errors

While trying to install Selenium, I never found a resource that had a simple install guide with everything I needed. That being said, I hope this can help someone who needs to install everything – and if it doesn’t, well at least it will help me if I need to install them on another computer ;)

This post assumes PHP is installed (I used 5.2.5) in C:\php\ (change the directory where applicable).

Installation

The first thing you’ll need to do is to install PEAR. To do this, Open up your command prompt. You can do this by:
clicking the start button
typing “cmd” (without quotations) in the search field
Click the “cmd.exe” application or Hit ENTER if that’s the only one there

In the command prompt, go to your PHP root directory. Mine was located at C:\php\
cd C:\php\

Run the go-pear.bat file with the command prompt. This batch file will install PEAR in C:\php\PEAR\

Next, you’ll want to install PHPUnit. To do this, type the following in the command prompt:
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit

The first command registers the pear.phpunit.de channel with the local PEAR environment – basically it lets PEAR know where to get PHPUnit from. The second command downloads and installs PHPUnit.

After it installs, you’ll be able to find it in C:\php\PEAR\PHPUnit\

Next, you’ll want to download the Selenium package using pear. As of this writing, the newest version is 0.4.3. In the command prompt, type:
pear install Testing_Selenium-beta

At this time, if you don’t have Java installed, you should install it now: http://www.java.com/en/download/manual.jsp#win
To check if you have java installed, in the Command prompt type “java -version” (you need JRE 1.5 or higher).

After that, you can install Selenium RC. I downloaded the most recent at this time; Version 1.0 beta 1: http://selenium-rc.openqa.org/download.html
Create a new folder called “selenium” somewhere easily accessible by the command prompt.
Inside the folder you downloaded there’s a Selenium-server folder (Mine was called “selenium-server-1.0-beta-1″). Move all the files in the Selenium-server folder into the “selenium” folder you created, mentioned above.

In the Command prompt, go to the directory that contains the Selenium server.

To start the server in interactive mode (allows you to control the browser manually), type the following in the command prompt:
java -jar ./selenium/selenium-server.jar -interactive

And finally everything is set up to create and run tests!

To create tests, you can use the Firefox plugin IDE to record actions. Alternatively, you can code them yourself.

If you use the IDE: After you create a new test, export it as PHP to an easy-to access location.
In the command prompt change your directory to the location of your new test.
use the following to run the test using PHPUnit:
phpunit testname.php

Assuming you’ve done everything correctly, PHPUnit will read the test file and tell your running Selenium RC server to open a browser. From there, it will go through your test and give you a report!

Note: This post was originally published on my old blog on August 28th, 2008 and was re-posted here for safe-keeping.

Linux bridged network VMware Fusion OSX

Bridged network configuration with VMware Fusion 3 and a Macbook Pro’s Internal Wireless Card

For one of my development set ups, I use a virtual machine of CentOS through VMware Fusion 3 on a host of Mac OSX Snow Leopard running on a Macbook Pro.

My host uses the Airport (Wireless card) for its internet and my VM needs access to its internet to push to the development Git repository. My host also needs access to my Virtual Machine’s web server for testing the site before committing.

Pretty basic setup, all in all… but it did take me awhile to configure the first time, so here it is:

I have VMware Fusion configured as a Bridged network with the internal wireless card. This will bind my Host’s wireless card (en1) to the ethernet of the virtual machine (eth0).

Note: The wireless card will be interpreted as an ethernet port in the Virtual Machine. If you want it to be interpreted as a wireless card, then you’ll have to use a USB wireless card.

1. Configure the network interface, set the IP address and the netmask:

ifconfig eth0 up 192.168.0.200 netmask 255.255.255.0

2. Add a route to the default gateway (router)… 192.168.0.1 is my router’s IP and eth0 is my card from the virtual machine:

route add default gw 192.168.0.1 eth0

3.
a) Add our DNS entry so we can resolve host names to IP addresses:

vi /etc/resolv.conf

b) Add the following to the /etc/resolve.conf file:

nameserver 192.168.0.1

Save, quit vi.

Check to make sure it worked!


ping google.ca

And there we have it!

This information was found from the following link, and this may offer more details if you need it:

http://www.fdlinux.com/networksetuphowto.html

That’s all you need for networking, but there’s one more step to allow the host to access the virtual machine’s website.

4. Edit the /etc/hosts file to resolve the chosen name to the VM’s web server:

192.168.0.200 local.dev.com

Now, when typing in “local.dev.com” in a web browser on the host’s machine it will load the virtual machine’s web server… assuming your virtual machine’s web server is configured correctly (which is beyond the scope of this article).

Git Delete Commit

To keep myself from looking when it happens again, here’s a personal reference of how to delete a commit:

If you made a commit, but have not pushed it to others:

git reset HEAD^

OR

git reset HEAD~1

both HEAD^ and HEAD~1 are shortcuts for the commit before head.

The above command will make sure you don’t lose your changes – instead, the commit will be deleted and your files will state they are locally modified. It’s the same thing as doing git reset –soft HEAD^

To delete your commit as well as ALL local changes including any staged, uncommitted, changes (remember, only if you haven’t pushed it to others):


git reset --hard HEAD^

If you already pushed your commit, you need to create a new commit that reverses your mistake.


git revert HEAD

Going green with electronics

Solar-Powered USB Charger

A few months ago I built a solar powered iPhone charger for a friend’s birthday – it turned out great!

The solar panel is 110mm x 140mm. It outputs 6V at 330 mA and charges a lithium ion polymer battery with a capacity of 1200mAh (total of about 4.5 Wh) and a nominal output of 3.7V.

Solar-Powered USB Charger Solar-Powered USB Charger

Christmas Post

Being the 24th of December, I thought it would be the perfect chance to give myself a gift – a new blog!

I’ve been planning it for awhile, I just hadn’t had the time until now (or so I keep telling myself…).

This blog will mainly serve the purpose of storing information I don’t want to forget, solutions to coding problems I’ve had and resolved, or just my general insane ramblings from time to time (though I do try to keep those on the down-low and off the internet).

Well, I hope you find the posts on my blog informative or interesting to read and I hope that you find whatever it was you were looking for when you landed here :-)

Merry Christmas and Happy New Year…or whatever season it is when you’re reading this ;-)