htop – top on steroids

March 19th, 2009

htop is a much heavier and feature rich version of ‘top’

Simply apt-get install htop and you’re done. You can increase or decrease the scheduling priority of running processes (see renice), you can send signals to running processes applications and view a comprehensive list of resource usage details.

Copy files securely from host to host with SCP

March 17th, 2009

SCP is a really handy utility for copying files to or from your machine to another host over SSH.

Usage:

scp /tmp/myfile user@otherhost:/tmp/
Will send /tmp/myfile over to /tmp/ on otherhost

scp user@otherhost:/etc/passwd ~/
Will copy /etc/passwd from otherhost to my home directory (~/)

Like everything in Linux, it’s that easy 🙂

As SCP uses SSH as it’s transport, key authentication will allow a passwordless login.

Linux SCREEN Command

March 14th, 2009

To start with, apt-get install screen on your favorite Debian server.

For the purposes of this tutorial (and throughout the site), ‘^C’ refers to Ctrl+C, ‘^A’ to Ctrl+A etc.

Now run screen with: screen

You are now within a virtual terminal. Typing exit will close your virtual terminal, and as it is the only virtual terminal open, also terminate the screen command.

Run screen again. Now within the screen type watch -n 1 ps aux – although outside the scope of this screen tutorial, this command will issue ‘ps aux’ to show the running process list every second. Let’s assume that we want to leave this running. Now type ^A, D – this will detach from your screen and you should see “[detached]” on your terminal. You are now back to your terminal, with screen still running.

Type screen -x to reconnect back to your screen session, and you will notice that your watch/ps processes are still running.

Type ^A, C and you will create a new ‘window’ within your same screen session. You can create as many windows as you wish, and the type exit to close them.

You can use ^A, 0 where 0 is your window number to switch between windows within your screen. When your last window is closed, screen will terminate.


Contributed by Reader Phil:

* CTRL + A + p for previous screen
* CTRL + A + n for next screen
* CTRL + A + A to name screen
* CTRL + a + S split screen
* CTRL + a + TAB change screen
* CTRL + a + q close split screen

And you also can modify your .screenrc to add a status bar:
hardstatus alwayslastline
hardstatus string ‘%{= kG}[ %{G}%H %{g}][%= %{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]‘

I hope you’ve found this helpful

Simple MySQL Developer Intro

March 11th, 2009

MySQL is one of the most powerful and widely used databases available. Here is a really quick guide to creating a database, creating a table, inserting, selecting and deleting the data, then table, then database. This will not go into too much depth as there are plenty of resources out there already that can provide more information on each step.

I’ll be using the MySQL command line tool on a Linux (Debian!) platform. Assuming I already have mysql running and a passworded root user account:

ns3:~# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or g.
Your MySQL connection id is 168753
Server version: 5.0.51a-24-log (Debian)
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.

mysql>

Read the rest of this entry »

umount: device is busy

March 9th, 2009

umount: /tmp/disk: device is busy

This is a common problem when trying to unmount a filesystem that is currently in use, especially when you have no idea what is using it!

Here’s a test..

ns3:~# cd /tmp/disk

Now we’ll create a test.sh script that will simply loop indefinitely, pausing every second as it goes.

ns3:/tmp/disk# cat test.sh
#!/bin/bash

while(true); do
sleep 1;
done

ns3:/tmp/disk# chmod +x test.sh

ns3:/tmp/disk# ./test.sh &
[1] 31460

Now test.sh is running, I’ll return to my home directory

ns3:/tmp/disk# cd ~/

Attempting to unmount /tmp/disk returns:

ns3:~# umount /tmp/disk
umount: /tmp/disk: device is busy
umount: /tmp/disk: device is busy

Assuming I don’t know that test.sh is currently running:

ns3:~# lsof +D /tmp/disk
COMMAND   PID USER   FD   TYPE DEVICE SIZE NODE NAME
test.sh 31460 root  cwd    DIR    7,0 1024    2 /tmp/disk
test.sh 31460 root  255r   REG    7,0   44   12 /tmp/disk/test.sh
sleep   31666 root  cwd    DIR    7,0 1024    2 /tmp/disk

Now I know that test.sh is running, I can issue:

ns3:~# killall test.sh
[1]+  Terminated              ./test.sh  (wd: /tmp/disk)
(wd now: ~)

Followed by:

ns3:~# umount /tmp/disk

How to create a simple disk image formatted to ext3

March 8th, 2009

We want a 48MB image, formatted to ext3

ns3:/tmp# dd if=/dev/zero of=./disk.img bs=1MiB count=48
48+0 records in
48+0 records out
50331648 bytes (50 MB) copied, 0.301372 s, 167 MB/s
ns3:/tmp# mkfs.ext3 ./disk.img
mke2fs 1.41.3 (12-Oct-2008)
./disk.img is not a block special device.
Proceed anyway? (y,n) y

ns3:/tmp# mkdir disk

ns3:/tmp# mount -oloop ./disk.img ./disk
ns3:/tmp# df -h ./disk
Filesystem            Size  Used Avail Use% Mounted on
/tmp/disk.img          47M  4.8M   40M  11% /tmp/disk

That’s it – now we can copy our content to ./disk before unmounting it, then use dd to write it to our target medium (such as a CF card or similar)

ns3:/tmp# umount ./disk

Linux piping and redirecting stdin stderr stdout

March 7th, 2009

We have three relevant streams when dealing with passing data around on the command line. STDIN (0), STDOUT (1) and STDERR (2)

echo “hello” will return “hello” to STDOUT

echo “hello” | sed s/llo/y/g
Returns: ‘hey’

echo “hello” will print “hello” to STDOUT which we pipe to sed’s STDIN. The shell will fork both processes, echo and sed, and create a pipe between one’s STDOUT to the other’s STDIN. A ‘broken pipe’ will occur when one terminates unexpectedly.

strace echo “hello” will print the system calls that the command makes. Lets say I just want to print out open() calls.

strace echo “hello” | grep open does not work. It seems that the grep is ignored.

This is because strace sends it’s output to STDERR and not STDOUT. In this case we must redirect STDERR to STDOUT so grep can pick it up on it’s STDIN.

strace echo “hello” 2>&1 | grep open will work successfully.

What if we want to redirect STDOUT and STDERR to a file? We simply redirect STDOUT to a file and then redirect STDERR to STDOUT.

strace echo “hello” >/tmp/strace.output 2>&1

A nonstandard method of achieving the same by redirecting everything in one go is strace echo “hello” &>/tmp/strace.output however this is not guaranteed to work across all implementations.

* Post edited thanks to observations from Adam Bolte (16/11/09)

Linux diff and patch

March 5th, 2009

diff and patch are a complimentary pair of commands for viewing and applying changes to a file. Let’s assume that we have a simple C program (hello.c):

ns3:~/test# cat hello.c
#include &lt;stdio.h&gt;<br />
int main(void)
{
printf("Hello Worldn");
return 0;
}

Read the rest of this entry »

BASH Shell Scripting – Sort a string alphabetically

March 5th, 2009

I was asked today how to sort a string alphabetically with BASH

Using perl, you can easily enough use

print (join “”, sort split //,$_)

With bash however, the best option is:

echo “teststring” | grep -o . | sort -n |tr -d ‘n’; echo
Which returns: eginrssttt

A good way of enumerating each character from a string in general is:

for (( i = 0; i < ${#str[@]}; i++ )); do echo “${str[$i]}”; done

Linux Command History

March 4th, 2009

By default your history is stored under bash for 500 commands. You can view your history by issuing the command:

history

This is one good reason why you should not enter passwords as part of a command line where possible. i.e. use mysql -u root -p and then enter the password when prompted by the mysql command, rather than using mysql -u root –password=secretpassword

On login you can use unset HISTFILE which will cease logging to your history file.

You could also use rm -f ~/.bash_history; ln -s /dev/null ~/.bash_history

This will remove your bash history file, then link a file of the same name to /dev/null which is where your history logs will now end up!

bc – Linux command line calculator

March 3rd, 2009

bc is a great command line calculator for Linux. Under Debian based distributions just:

apt-get install bc

Read the rest of this entry »

How to upgrade from Debian Etch to Lenny

March 2nd, 2009

Debian Lenny is now stable, so here is a quick guide to upgrading. I would recommend taking backups before doing this, and not performing this upgrade on a live/production machine.
Read the rest of this entry »