Skip to content

            Subscribe (RSS | E-Mail)

Unix/Linux: Finding and Killing Processes by User

If you’ve ever run ps aux | grep user to list processes and hunt for process IDs, you’ll be happy to know there is a simpler alternative. Both finding and killing processes owned by a particular user is made simple using the handy pgrep and pkill utilities.

Listing Processes with pgrep

Listing all the processes owned by the user raam can be done like this (the -l switch causes the output to include the process name):

$ pgrep -l -u raam
9614 screen
9628 bash
9644 irssi
16165 bash
16297 rtorrent
19462 ssh
19515 bash
19526 ssh
20964 sshd

You can also filter the list of results by appending a full (or partial) process name to the command:

$ pgrep -l -u raam bash
9628 bash
16165 bash
19515 bash

Killing Processes with pkill

The pkill command does basically the same thing as pgrep, except it kills the processes instead of listing them. This is useful if you have a user with several dead processes, or if you were deleting a user and you wanted to kill any running processes first.

Killing all the processes owned by the user raam looks like this:

$ pkill -u raam

And once again, if you only wanted to kill all the bash processes owned by raam, you would append the process name to the command:

$ pkill -u raam bash

As always, check the man pages for pgrep and pkill for more information and switch options.


Posted in Linux.

Tagged with , , .


Mac OS X: Resizing Oversized Windows to Fit the Screen

I recently switched from a 15″ MacBook Pro (2007 model) to the latest 13″ MacBook Pro (2009, unibody model). After transferring my iTunes library to the new laptop, I found that the size of the iTunes window exceeded the size of the screen (the 15” MBP had a 1440×900, while the 13” MBP has a slightly smaller 1280×800). This meant I was unable to grab the bottom right corner of the window to resize it:

iTunes too large to resize!

Unfortunately, the maximize button in iTunes only cycles through Previous Size and the Mini Player mode; at no point does it actually maximize the window to fit the screen!

Note: As Ondrej commented below, you might be able to fix this by simply holding down the Alt/Option key and pressing the zoom/maximize button (the green button).

I tried installing an application called RightZoom which changes the behavior of the maximize button in OS X by maximizing the window to fill the screen (as opposed to simply maximizing for “best fit”). However, that did nothing to change the behavior of iTunes.

After searching the web for solutions and finding nothing, I remembered a trick I had used in the past on Windows computers: Change the display resolution to something smaller, thereby forcing the operating system to resize windows to fit the new resolution. Then, after the OS resizes the windows, change the resolution back to the original. Of course this only works if the original resolution is not already set for the lowest one (it most likely isn’t).

To my relief, this trick worked perfectly! When I switched to a smaller resolution, the iTunes window was resized to fit the smaller screen.

For your reference, here are the steps:

  1. Launch System Preferences -> Displays
  2. Select a smaller resolution
  3. iTunes (or any other application) will be resized to fit the smaller resolution
  4. Change the resolution back to the default
  5. Oversized window problems solved!

Posted in OS X.

Tagged with , .


MAC Address Validation Regex with egrep

I needed an easy way to validate a MAC address in a bash script that generated a unique hostname based on the MAC address of the system. This gem did the trick:

echo "00:11:24:3e:a5:78" | egrep "^([0-9a-fA-F]{2}\:){5}[0-9a-fA-F]{2}$"

In the event that there was a problem getting the MAC address (e.g., faulty NIC or unstable device driver), I generate a random hostname instead of basing the hostname generation on the MAC. Here’s how I validated the MAC in the script:

if [ `echo $ACTIVE_INTERFACE_MAC | egrep "^([0-9a-fA-F]{2}\:){5}[0-9a-fA-F]{2}$"` ]; then
	# generate unique hostname based on MAC
else
	# generate random-character hostname
fi

Posted in Bash, Linux, Regex.

Tagged with , , , .


A Script to Install & Configure ifplugd on Debian

The default configuration on some older Linux systems is to only send a DHCP request while booting up. This means if the network cable gets unplugged, or if the router is powered off, the system may lose its IP configuration. To restore the network connection, the system may need to be manually rebooted or have someone at the local console run the dhclient command to request a DHCP lease.

For systems that are only accessed remotely via SSH, such a scenario can be painful. What is needed is a daemon that watches the link status of the Ethernet jack and reconfigures the network (or sends out another DHCP request) when it detects a cable is plugged in (or the power to the router is restored).

ifplugd does exactly that:

ifplugd is a Linux daemon which will automatically configure your ethernet device when a cable is plugged in and automatically unconfigure it if the cable is pulled.

On a Debian system, installing and configuring ifplugd is relatively simple using apt-get install ifplugd. Once its been installed, it needs to be configured by editing /etc/default/ifplugd. The most basic configuration is to simply set INTERFACES="auto" and HOTPLUG_INTERFACES="all". This configuration tells ifplugd to watch all network interfaces for a new link status and automatically reconfigure them using the Debian network configuration defined in /etc/network/interfaces.

I recently needed to automate the install and configuration of ifplugd on many remote Linux systems, so I wrote this simple script.

Download: install-ifplugd.tar.gz

#!/bin/sh

#########################################
# Author: Raam Dev
#
# This script installs ifplugd and configures
# it to automatically attempt to restore any
# lost connections.
#
# Must be run as root!
#########################################

# Check if we're running this as root
if [ $EUID -ne 0 ]; then
   echo "This script must be run as root" 1>&2
   exit 1
fi

# Files used when configuring ifplugd
OUTFILE=/tmp/outfile.$$
CONFIG_FILE=/etc/default/ifplugd

# Update package list and install ifplugd, assuming yes to any questions asked
# (to insure the script runs without requiring manual intervention)
apt-get update --assume-yes ; apt-get install --assume-yes ifplugd

# Configure ifplugd to watch all interfaces and automatically attempt configuration
sed 's/INTERFACES=\"\"/INTERFACES=\"auto\"/g' < $CONFIG_FILE > $OUTFILE
mv $OUTFILE $CONFIG_FILE

sed 's/HOTPLUG_INTERFACES=\"auto\"/HOTPLUG_INTERFACES=\"all\"/g' < $CONFIG_FILE > $OUTFILE
mv $OUTFILE $CONFIG_FILE

If you’re interested in doing more with ifplugd, check out this article.


Posted in Debian, Linux, Networking.


Configuring Kayako SupportSuite to Close Tickets via Email

If you’re using Kayako SupportSuite to process emails and turn them into tickets, you might also be interested in being able to close tickets via email. This would allow a staff or regular users to close tickets by simply responding to the email notification with something like “close this request” in the body of the message. Using a mail parsing rule, this is relatively easy to set up in SupportSuite.

First, login to the admin section of your SupportSuite installation. Now insert a new mail parser rule by selecting Mail Parser » Manage Rules » Insert New Parser Rule in the left navigation menu. Now configure the parsing rule as follows:

Rule Title: Close Ticket via Email
Stop processing rules: No
Execution Order: 1

Now add three criteria (press the green plus icon to add more than one criteria).

Body » Contains » close this request
Plain-text Body » Contains » close this request
HTML Body » Contains » close this request

You can change ‘close this request’ to whatever you like, but keep in mind it should be something that isn’t likely to occur in a ticket response (i.e., ‘close’ would probably be too generic).

If you have more than one email queue and you only want this mail parser rule to apply to a specific queue, you can add a forth (optional) criteria:

Destination E-mail Address: [mail queue address, i.e., support@mycompany.com]

Now finish configuring this parser rule:

Select Match Any Criteria
Set Rule Type: Post Parse
Change Ticket Status: Closed

That’s it! Now press Insert and you’re done.


Posted in Kayako SupportSuite, Software.