Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Thursday, July 03, 2014

Bash trick to reference all args from previous command

!!:1- is a nice shortcut for referencing all arguments from the previous bash command.

command                equivalent
---------------------------------------
ls                           
sudo !!                    sudo ls
!$                            ls
ls 1.txt 2.txt           
ls !$                        ls 2.txt
ls 1.txt 2.txt           
cat !!:1-                  cat 1.txt 2.txt  

Tuesday, April 28, 2009

Useful wireshark startup options

Sometime when looking at TCP/IP traffic on multiple ports it's useful to start up several wireshark instances and have each capture traffic on distinct port, and give the window a specific title:


sudo wireshark -o "gui.window_title:Data Server on $SERVER_PORT -" -i lo -p -f "port $SERVER_PORT" -k &


-i lo
capture packets on local loopback interface

-p
don't go into promiscuous mode

-f "port $SERVER_PORT"
use the following filter (specific port)

-k
start capture immediately

Thursday, April 23, 2009

How to print process environment variables

Here is a script that prints process environment variables given process ID in Linux:


#!/bin/bash
if [ $# -ne 1 ] ; then
echo "Usage: `basename $0` process_id"
echo "Prints process environment variables"
exit 1
fi

sed 's/\o0/\n/g' /proc/$1/environ

Wednesday, March 11, 2009

Running GUI programs remotely

It used to be that you would set the DISPLAY variable
and use xhost to run GUI programs on remote machine
and send traffic to your desktop. It seems the modern
way is to use ssh tunneling for X11 traffic.

This is accomplished by simply running:

ssh -fY host program

E.g.:

ssh -fY user@hostname xclock

Internally ssh establishes a connection to remote machine,
starts a proxy server listening to port (e.g. 6010),
sets the display variable (e.g. DISPLAY=localhost:10.0),
and starts the requested program (e.g. xclock).
xclock sends all X11 traffic to the server spawned
by the ssh process, and forwards it to your desktop's X server.

Tuesday, January 06, 2009

Trapping errors in bash

Bash has functionality similar to exceptions in java.
If we want to handle any sort of error, we can add a handler function
and exit with an error code. This sure beats checking exit code
after every command.

function error {
echo Error
exit 1
}
trap error ERR

Tuesday, December 30, 2008

Ubuntu gnome session management

Ubuntu 8.04 gnome session manager is very unreliable and unintuitive. You can hardly accomplish anything using the UI. It is amusing that while one can think of it as a helper program to help you restore your programs between system restarts it can also be used to kill programs currently running on your computer, unexpectedly. The author clearly had no idea of how UI should be designed in general, nor a plan for what he was trying to get accomplished with this program specifically. It is the single worst piece of software included with Ubuntu. Oh well one of these days I'll write a better one and get it included with Ubuntu.

However you can partially turn off this useless software by clicking "Automatically remember..." in the Session Options Tab and instead edit the file ~/.gnome2/sessions by hand.

E.g. I added this program to get started automatically with my X session:
16,RestartCommand=/usr/bin/gvim /home/dmitri/Documents/Status.txt /home/dmitri/Misc/GTD.txt

Unfortunately, this displays a strange error message in VIM. The error message only has an error icon and the path to the file. However after I click on it, all the files are opened just fine, except the second one is opened twice. Oh well, it's still 2008 I guess: it's 2009 that will be the year of the Linux desktop.

Tuesday, September 30, 2008

Fixing java disk swapping problem on linux

Running JDK 1.6 on my ubuntu hardy laptop I noticed hard disk drive light flashing every second. Disk activity was happening when running any java program, e.g. Eclipse, or Tomcat. I pinned this down to the JVM's performance files in the /tmp/hsperfdata directory. To turn off disk swapping, and prolong your battery and disk live, run JVM with the following option -XX:-UsePerfData

E.g. for Eclipse you may edit eclipse.ini and add this line at the end:
-XX:-UsePerfData

These were the steps I took to diagnose the issue:

# Become root
sudo su -

# Stop loggers
/etc/init.d/sysklogd stop
/etc/init.d/klogd stop

# Enable block I/O debugging
echo 1 > /proc/sys/vm/block_dump

# Monitor kernel ring buffer
while true; do dmesg -c; sleep 1; done;

...
Examine the output
...
[ 562.539286] java(10323): dirtied inode 8224835 (10310) on sda1
[ 562.539293] java(10323): dirtied inode 8224835 (10310) on sda1
[ 562.545651] eclipse(13917): dirtied inode 8224819 (13898) on sda1
[ 562.545658] eclipse(13917): dirtied inode 8224819 (13898) on sda1

# Find out filename from inode
find / -inum 8224819 2> /dev/null
/tmp/hsperfdata_dmitri/13898

# We're done
# Turn off block I/O debugging
echo 0 > /proc/sys/vm/block_dump

# Start loggers
/etc/init.d/sysklogd start
/etc/init.d/klogd start