minuteware

minute discoveries

Disconnect user’s SSH session

| Comments

With my VPS I sometimes encounter a situation when I try to open a file with Vim, but it tells me the file has already been opened by someone else. This “someone else” is another myself, logged in via SSH from my laptop, which has been suspended (SSH sessions are not being disconnected when you suspend). An easy way to disconnect myself, logged in from the laptop, is using pkill.

1
$ pkill -KILL -u <username>

That’s it. SSH session has been disconnected and I can now open the desired file.

Qt apps with GTK theme in Xubuntu 11.10

| Comments

As you may have noticed, Qt apps no longer use GTK+ theme in Xubuntu 11.10. For me this became particularly noticeable in Psi+. The problem is caused by QGtkStyle, which cannot find GTK+ theme since Xfce stores it in an unusual location (not sure why 11.04 could find it though).

To fix the problem add the following to ~/.config/xfce4/gtkrc (create the file if it does not exist):

1
gtk-theme-name = greybird # Change 'greybird' to whatever Xfce theme you're using

Create ~/.config/xfce4/xinitrc and add the following to it:

1
2
3
4
#!/bin/sh
GTK2_RC_FILES=$HOME/.config/xfce4/gtkrc
export GTK2_RC_FILES
. /etc/xdg/xfce4/xinitrc

After relogin all Qt apps will use your current Xfce GTK+ theme.

Multi-row window buttons in Xfce

| Comments

To make the Xfce Window Buttons applet (or simply the Taskbar, as all of us are used to call it) multi-row, add the following to ~/.config/xfce4/gtkrc (create the file if it doesn’t exist):

1
2
3
4
5
6
7
8
9
10
11
include "/usr/share/themes/greybird/gtk-2.0/gtkrc" # Change 'greybird' to whatever Xfce theme you're using
 
style "xfce-tasklist-style"
{
    XfceTasklist::max-button-length = 192
    XfceTasklist::max-button-size = 16 # Make sure that this value is at least two times less than the panel size
    XfceTasklist::ellipsize-mode = PANGO_ELLIPSIZE_END
    XfceTasklist::minimized-icon-lucency = 50
    XfceTasklist::menu-max-width-chars = 24
}
class "XfceTasklist" style "xfce-tasklist-style"

After you relogin, your panel should look like this:

multi-row

The allmighty `watch` Linux command

| Comments

I was searching for a way to make some kind of a live memory usage monitoring in Linux when I found watch. I wonder why didn’t I ever knew about it! It’s damn useful! You can monitor literally everything with watch.

Here are some scenarios you may use if for:

Watch for changes in a directory:

$ watch ls -la

Watch for any Java program being started/stopped:

$ watch -n 1 "ps ax | grep java"

I used watch for memory usage monitoring:

$ watch -n 1 free -m

$ watch -n 1 cat /proc/meminfo

Simulating high memory usage in Linux

| Comments

Doing some testing recently, I needed a way to eat up as much RAM as possible on a Linux machine. As it turned out, there are not as many ways to do that quickly and easily as one could have desired. I’ve found only two actually. The first one, using Bash arrays, is quite slow. The second one, using C malloc and memset functions, is quite insecure because you can end up in a completely unresponsive machine (its especially important if the machine is remote). It could be extended though, to support sleeps and the amount of memory to be allocated (also I had some problems with int type overflow if specifying huge amount of RAM), but not being familiar with C, I did not want to dive deep into the problem.

Trying to google a little harder I found another way to do that, similar to the malloc+memset one — C realloc function. Actually I was lucky to find a ready-to-use code on C on unix.com (thank you pludi). The only thing the code lacked was a sleep function to sleep for some time with memory allocated. I added it and here’s what I’ve ended up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//#######################################
// ClearRam
// Holt sich so viel Speicher wie in der Kommandozeile angegeben (wenn möglich)
// Erstellt: 20081218 sky
//#######################################
// Updated: 2011 Andriy Yurchuk
//#######################################
// Compilation:
// $ gcc memalloc.c -o memalloc
// Usage:
// $ ./memalloc 512 15
// will allocate 512 MB of memory and keep it allocated for 15 seconds
//#######################################

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>

unsigned char *ptr = NULL;
void sigint_handler(int);

int main(int argc, char **argv)
{
    unsigned long meg = 1024 * 1024;
    unsigned long i = 0;
    unsigned int toalloc = 1;
    unsigned int sleeptime = 0;
    unsigned int j = 0;

    if (argc == 2) {
        toalloc = atoi(argv[1]);
    }
    else if (argc == 3) {
        toalloc = atoi(argv[1]);
        sleeptime = atoi(argv[2]);
    }

    signal(SIGINT, sigint_handler);

    for (j = 0; j < toalloc; j++) {
        printf("Trying to allocate %u MB of RAM...", j + 1);
        ptr = (unsigned char *) realloc(ptr, (j + 1) * meg * sizeof(char));
        if (ptr == NULL) {
            printf("failed\n");
            free(ptr);
            return 1;
        }
        ptr[0] = 0;
        for (i = j * meg; i < (j + 1) * meg; i++) {
            ptr[i] = ptr[i - 1] + 1;
        }
        printf("success\r");
    }
    sleep(sleeptime);
    free(ptr);
    printf("\n");
    return 0;
}

void sigint_handler(int status)
{
    printf("\nCaught SIGINT\n");
    free(ptr);
    exit(1);
}

Fixing the ‘MOZILLA_FIVE_HOME not set’ in Ubuntu

| Comments

There is quite a lot of noise regarding this error in the Internet. It is being triggered by Eclipse’s (and Eclipse-based products’) SWT, whenever it tries to generate a ‘browser’ window (like the one used when you do git commit in Aptana).

The root cause of this error is that Eclipse’s SWT cannot find Mozilla’s Xulrunner, which it uses to draw ‘browser’ windows. It seems that the problem appeared with Firefox 3.0 (at least I’ve found a post of a guy who wrote he started to see this after his Firefox upgraded from 2.x to 3.0). For some reason SWT does not support Xulrunner of versions greater than 1.9.2.

Well, all the solutions found on StackOverflow, forums, bug trackers seem invalid. They all suggest to define the MOZILLA_FIVE_HOME environment variable and point it to the Xulrunner installation (wherever it’s installed, with the requirement that the directory should contain libxpcom.so). In my case the result was that even the downloaded and extracted old Xulrunner 1.9.2 refused to work.

Then, on the Eclipse SWT FAQ page I found out that you can define custom path to Xulrunner Eclipse should be using. This is done with org.eclipse.swt.browser.XULRunnerPath option. But my downloaded and extracted Xulrunner distribution refused to work this way.

Then I started searching for an Ubuntu package of that old Xulrunner 1.9.2 (thought it could be that the lib should actually reside in the lib path). Since Xulrunner seems no longer being distributed apart from Firefox (at least in Ubuntu), there is not separate Xulrunner package for Ubuntu in the repositories. But I was lucky to find one for Oneiric on Launchpad here

So, basically, the steps to fix the error are the following:

  • Download and install Xulrunner deb package from the Launchpad page above
  • Add -Dorg.eclipse.swt.browser.XULRunnerPath=/usr/lib/xulrunner-1.9.2.17 option to your eclipse.ini (AptanaStudio3.ini)

That should fix the problem.

Thanks to Bruno Carlin for the insight