Sep 072010
 

umount: device is busy
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

Sep 072010
 

Filename: disktest.sh

#!/bin/bash
# This script does a very simple test for checking disk space.

space=`df -h | awk ‘{print $5}’ | grep % | grep -v Use | sort -n | tail -1 | cut -d “%” -f1 -`

case $space in
[1-6]*)
  Message=”All is quiet.”
  ;;
[7-8]*)
  Message=”Start thinking about cleaning out some stuff.  There’s a partition that is $space % full.”
  ;;
9[1-8])
  Message=”Better hurry with that new disk…  One partition is $space % full.”
  ;;
99)
  Message=”I’m drowning here!  There’s a partition at $space %!”
  ;;
*)
  Message=”I seem to be running with an nonexistent amount of disk space…”
  ;;
esac

Sep 072010
 

It’s possible to use even more complicated syntax with regular expressions:

I used this in my FTP script for safety surveillor.

case “$1? in
+(start|run) ) /usr/app/startup-script ;;
@([Ss])top ) /usr/app/stop-script ;;
esac

?(pattern1 | pattern2 | … | patternn)
zero or one occurrence of any pattern

*( pattern1 | pattern2 | … | patternn)
zero or more occurrences of any pattern

@( pattern1 | pattern2 | … | patternn)
exactly one occurrence of any pattern

+( pattern1 | pattern2 | … | patternn)
one or more occurrence of any pattern

!( pattern1 | pattern2 | … | patternn)
all strings except those that match any pattern

Jul 062010
 

On Linux or Unix Systems you can use sftp like this:

sftp -oPort=1234 username@remote.servername.com
After authenticating, the following commands can be used. Any
paths should have quotes if they contain spaces.

Some standard commands and their definitions for command line SFTP include:

?	Get help on the use of SFTP commands.
bye	Close the connection to the remote computer and exit SFTP.
cd	Change the directory on the remote computer.
chgrp	Change the group associated with a computer file (chgrp system foofile).
chmod	Change the permissions of files on the remote computer.
chown	Change the owner of files on the remote computer.
dir	List the files in the current directory on the remote computer.
exit	Close the connection to the remote computer and exit SFTP.
get	Copy a file from the remote computer to the local computer.
help	Get help on the use of SFTP commands.
lcd	Change the directory on the local computer.
lls	See a list of the files in the current directory on the local computer.
lmkdir	Create a directory on the local computer.
ln	Create a symbolic link for a file on the remote computer.
lpwd	Show the current directory (present working directory) on local computer.
ls	Show the current directory on the remote computer.
lumask	Change the local umask value.
mkdir	Create a directory on the remote computer.
progress Toggle display of progress meter.
put	Copy a file from the local computer to the remote computer.
pwd	Show the current directory (present working directory) on remote computer.
quit	Close the connection to the remote computer and exit SFTP.
rename	Rename a file on the remote host.
rm	Delete files from the remote computer.
rmdir	Remove a directory on the remote host (the directory has to be empty).
symlink	Create a symbolic link for a file on the remote computer.
version	Display the SFTP version.
! 	In Unix, exit to the shell prompt, where you can enter commands. Enter exit
	to get back to SFTP. If you follow  !  with a command (e.g., !pwd), SFTP
	will execute the command without dropping you to the Unix prompt.
Jun 242010
 

Before we get started with the commands you may not have heard of before, here’s a tried and true Unix command that we’ve all been using for decades, but maybe not this particular way. This grep command represents the simplest way to remove blank lines from a file.

grep . oldfile > newfile

Got that? It matches any lines with anything (i.e., not nothing) in them and tosses them into the second file. Hey, this is even easier than my old “grep -v ^$ oldfile > newfile” command that would specifically omit empty lines from the new file
Another interesting command is called “tac”. Yes, that’s “cat” spelled backwards. And what would you expect such a command to do? It displays a file in reverse order — upside down. So, our a, b, c, x, a, b, c file shows as:

$ tac myfile
c
b
a
x
c
b
a
Jun 242010
 

Awk does a lot more than select a column from a file or an input stream. It can select columns from selected rows. It can calculate totals, extract substrings, reverse the order of fields and provide a whole lot of other very handy manipulations. Whether you squeeze your awk permutations onto the command line or prefer to build them into scripts, the language is clever and versatile and well worth using even as it joins the ranks of middle-aged computer utilities.

If you want to use awk to add up a bunch of numbers formulated as a column in a text file, you can use a one-liner like this:

$ awk ‘{ SUM+=$1 } END { print SUM }’ < nums
That SUM+= operation adds the contents of column one to a running total for each line of input. The sum is only printed at the very end when input is exhausted. You can change $1 to the column of your choice or $NF if you want to add up the rightmost column.

If you prefer scripts to the command line, you could use a script like the addcol script below to sum whatever column you choose. You would just pass the column you want to add on the command line as the COL parameter:

$ awk -f addcol COL=3 < numbers
The -f tells awk to run the designated script (addcol) and COL=3 passes “3” as the column number you want to sum.

# addcol
BEGIN { SUM=0 }
{
  print $COL
  SUM += $COL
}
END { print “Sum: ” SUM }
Awk has some grep-like features as well. If you want to operate only on lines that contain some particular text, you can specify that text on the command line like this:

$ awk ‘/choose me/’ textfile
You can also combine search options using && (and), || (or), and even negation operators. The command below selects lines that contain both the word “this” and the word “that”. Using ‘/this/ && !/that/’ would select only lines that contain “this” without containing “that”.

$ awk ‘/this/ && /that/’ notes
# find the process that started this one
# make sure that this user provided an answer
You can also select content based on its position within your input. In the command below, we only want to see lines eleven and above.

$ awk ‘NR > 10’ counts
11 63
12 99
13 63
14 77
15 41

And, of course, there are a lot of other nice little tricks available.

Awk provides some quick and effective filtering and incorporates an easy syntax. Probably the only thing that takes some getting used to is referring to parameters without putting dollar signs in front of them.

Obtained from

May 182010
 

I was having a problem with WordPress where I could not upload any documents to my website via posting and I could not activate Akismet or change Appearance or Themes. Basically, I could not write to the backend even though my MySql account had full privileges in the database. I got the following error message when I tried to enter my Akismet key:

Akismet The key you entered could not be verified because a connection to akismet.com could not be established. Please check your server configuration.

The problem was that Apache did not have read/write access to the blog website.

The solution was to change the owner of the blog from root to apache the restart the web service.

sudo chown -R apache:root /var/www/html/blog/

sudo service httpd restart

Once I did this my errors went away.

Apr 152010
 

Q. How do I clear the mysql command line history stored in ~/.mysql_history file?

A. mysql is a simple SQL shell. It supports interactive and non-interactive use.

On Unix and Linux, the mysql client writes a record of executed statements to a history file. By default, the history file is named .mysql_history and is created in your home directory. To specify a different file, set the value of the MYSQL_HISTFILE environment variable.

Task: CLEAR MYSQL COMMAND LINE HISTORY

Remove ~/.mysql_history by typing following command:
$ > ~/.mysql_history

If you do not want to maintain a history file, first remove .mysql_history if it exists, and then use either of the following techniques:

Set the MYSQL_HISTFILE variable to /dev/null. To cause this setting to take effect each time you log in, put the setting in one of your shell’s startup files.

Create ~/.mysql_history as a symbolic link to /dev/null. You need run following command only once:
$ rm $HOME/.mysql_history
$ ln -s /dev/null $HOME/.mysql_history