Karim Vaes

Why chop at leaves, when one must dig at roots
  • Home
  • About me
    • Curriculum Vitae
    • Services
    • Projects
  • Scripts
    • OWA Most Popular
  • Search
  • License
  • Contact

Find out who is monopolizing or eating the CPUs

When you need to determine which process is monopolizing or eating the CPUs. Following command will displays the top 10 CPU users on the Linux system.

ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10

or

ps -eo pcpu,pid,user,args | sort -r -k1 | less

Output
%CPU PID USER COMMAND
96 2148 vivek /usr/lib/vmware/bin/vmware-vmx -C /var/lib/vmware/Virtual Machines/Ubuntu 64-bit/Ubuntu 64-bit.vmx -@ ""
0.7 3358 mysql /usr/libexec/mysqld --defaults-file=/etc/my.cnf --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pid-file=/var/run/mysqld/mysqld.pid --skip-locking --socket=/var/lib/mysql/mysql.sock
0.4 29129 lighttpd /usr/bin/php
0.4 29128 lighttpd /usr/bin/php
0.4 29127 lighttpd /usr/bin/php
0.4 29126 lighttpd /usr/bin/php
0.2 2177 vivek [vmware-rtc]
0.0 9 root [kacpid]
0.0 8 root [khelper]

If you’re interested in more information on Linux CPU utilization then check out the related article on nixCraft here.

Comments
No Comments »
Categories
Bash, Unix/Linux
Comments rss Comments rss
Trackback Trackback
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 3.00 out of 5)
Loading ... Loading ...

Wrong password doofus!

There is a fun feature available within the sudo program. You’ll get an insult when you do the wrong thing such as enter your password incorrectly.

To turn the feature on you’ll need to use the following command:

sudo visudo

(always use visudo when you need to edit your sudoers file as it has a self-check system that won’t let you screw it up.)

Change the “Defaults”, by adding “,insults to the back
For example:

Defaults !lecture,tty_tickets,!fqdn,insults

Save, and enjoy the action!
(Note: use “sudo -K” to clear your sudo session)

Referenced original

Comments
No Comments »
Categories
Bash, Fun, Ubuntu, Unix/Linux
Comments rss Comments rss
Trackback Trackback
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

my own setlock source code

I’ve added the “force” options, as I needed it for the project I’m working on.

#!/bin/bash

usage()
{
  echo "usage: setlock [ -fFnN ] [ -xX ] [ -v ] [ -V ] file program [ arg ... ]"
  echo " Options"
  echo "    -f: No delay. If fn is locked by another process, try to force ownership by trying to kill the process that has the lock."
  echo "    -F: No delay. If fn is locked by another process, bruteforce ownership by killing the process that has the lock (-9)."
  echo "    -n: No delay. If fn is locked by another process, setlock gives up."
  echo "    -N: (Default.) Delay. If fn is locked by another process, setlock waits until it can obtain a new lock."
  echo "    -x: If fn cannot be created or locked, setlock exits zero."
  echo "    -X: (Default.) If fn cannot be created or locked, setlock prints an error message and exits nonzero."
  echo "    -v: Verbose messaging"
  echo "    -V: Show the version"
  exit 1
}
version()
{
  echo "Version 0.1"
  usage
}
verbose()
{
  if [ "$VERBOSE" = "y" ] ; then
    echo "$1"
  fi
}
cleanup()
{
  if [ "$SETLOCK" = "y" ] ; then
    killlock
    clear_lock
  fi
}
trap "cleanup"
set_lock()
{
  verbose "set lock"
  echo $$ >> $LOCK
  SETLOCK=y
}
clear_lock()
{
  verbose "Clear lock"
  rm $LOCK >/dev/null 2>&1
}
killlock()
{
  pids=`cat $LOCK 2>/dev/null`
  for pid in $pids
  do
    verbose "Kill $1 on process $pid"
    kill $1 $pid >/dev/null 2>&1
  done
}
run_command()
{
  set_lock
  verbose "Running command"
  eval bash -c \"$CMD\"
  RET=$?
  verbose "Command done - exit value = $RET"
  clear_lock
  RET=$?
  exit $RET
}
no_wait()
{
  verbose "No waiting"
  exit $EXITCODE
}
wait_nice()
{
  verbose "Waiting nice"
  while [ -f "$LOCK" ] ; do
    sleep 1
  done
  run_command
}
force_kill()
{
  killlock
  run_command
}
force_brute()
{
  killlock -9
  run_command
}

FORCE=n
DELAY=y
EXITCODE=1
VERBOSE=n
OPTS=0
while getopts "f F n N x X v V" o ; do
  case $o in
    f) FORCE=y DELAY=n;;
    F) FORCE=b DELAY=n;;
    n) DELAY=n;;
    N) DELAY=y;;
    x) EXITCODE=0;;
    X) EXITCODE=1;;
    v) VERBOSE=y;;
    V) version;;
  esac
  let OPTS=$OPTS+1
done

if [ $# -lt 2 ] ; then
  verbose "Not enough parameters given"
  usage
fi

APTS=0
while [ $APTS -lt $OPTS ]  ; do
  shift
  let APTS=$APTS+1
done

LOCK=$1
shift
CMD=$*

if [ -z "$LOCK" ] ; then
  verbose "No lockfile given"
  usage
fi

if [ -z "$CMD" ] ; then
  verbose "No command given"
  usage
fi

if [ -f "$LOCK" ] ; then
  if [ "$DELAY" = "y" ] ; then
    wait_nice
  else
    case "$FORCE" in
      "n") no_wait;;
      "y") force_kill;;
      "b") force_brute;;
      *) exit 99;;
    esac
  fi
else
  run_command
fi

Feel free to use it, yet I’m publishing it here under the GPL license

Comments
No Comments »
Categories
Bash, Unix/Linux
Comments rss Comments rss
Trackback Trackback
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

bash : locking issues – example with setlock

Something that kept me busy for a while. An internal (bash) framework sometimes (Isn’t that great! -sigh-) gave unexpected results. Sometimes the timings were too long, and sometimes data was missing. In the end, it was due to a locking issue. A program tried to execute something while forcing an internal lock. Yet the program was waiting until the lock was released. But when in some cases when two programs do this, this causes a deadlock.

The code below is a way to get around this deadlock. It’s an example using “setlock”, yet it shows the concept. Instead of waiting for the program to release the lock (default behaviour with “setlock”), you can script a small loop that comes back after a few seconds. Yet you have to setup the locking mechanism in a manner that it won’t wait for the lock (option -n for “setlock”).

SUME="bash -c "
SETLOCK="path-to-setlock"
EXECUTE="your-script"
PARAMETERS="your-parameters"
ID="your-unique-id-for-locking"
# usage of a control structure with non-wait locking
# this is done to avoid locking issues ^^
var0=0
LIMIT=3
SNOOZE=5
while [ "$var0" -lt "$LIMIT" ]
do
  var0=`expr $var0 + 1`
  eval $SUME \"$SETLOCK -n $TMPDIR/lock_$ID $EXECUTE $PARAMETERS 2>/dev/null \"  2>/dev/null
  EC=$?
  if [ $EC -eq 0 ] ; then
     var0=`expr $LIMIT + 1`
  else
     sleep $SNOOZE
  fi
done
exit $EC
Comments
No Comments »
Categories
Bash, Unix/Linux
Comments rss Comments rss
Trackback Trackback
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

bash getopts

If you’re scripting with bash, and haven’t encountered the getopts function, then you should read the following
article @ linux.com.

The getopts function is an -easy to use- function when you’re working with script where parameters and flags have to be set.

Example

while getopts ":f:p:c:s:d r v" o ; do
        case $o in
                f ) FILE=$OPTARG;;
                p ) PARA=$OPTARG;;
                c ) VALU=$OPTARG;;
                s ) DELI=$OPTARG;;
                d ) DEBUG=y;;
                r ) RO=y;;
                v ) echo $VERSION
                    exit 0;;
        esac
done

The options with a colon (“:”) in front of them need to have an argument set. Yet if there is a space, then no argument is needed.

So -r -d & -v are the flags. Where the others are options/parameters that can be set.

Comments
3 Comments »
Categories
Bash, Unix/Linux
Comments rss Comments rss
Trackback Trackback
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

« Previous Entries

Recent Posts

  • Why IT shouldn’t be run as a business…
  • Sushi Etiquette
  • Wage cut or wage freeze?
  • Basic business money making concepts
  • Quote of the day

Similar Posts

  • Vmmon Issues with ubuntu gutsy gibson
  • The Virtualization options in Linux
  • Tips & Tricks : Finding your monitor settings for your xserver config
  • The popularity of Ubuntu
  • Installing Asterisk on a Linksys WRTG

Recent Comments

  • BloggerGeeze on Wordpress widget : Most popular posts
  • linux-blog - Fa. anracon - Dr. Mönchmeyer » Blog Archive » Dell M90, Suse 11.2, KDE 4.4, VMware - Teil II on Running your dual boot windows inside Vmware Server within Ubuntu
  • Khalid Inayatullah on Raid Levels
  • Söve on Disabling the openoffice splash screen in ubuntu
  • alidhaey on BigIP LTM : configuring & testing the snmp destinations

Commercial

Categories

2.0 Ads Agile Bash Blogroll Book Brain Business Career Change CIO Collaboration Communication Corner Creative CRM Culture Desktop Development Dreambox Drupal Education Entrepreneur F5 Firefox Food Freelance Fun General Green Growth High Availability Human Resources Idea Infrastructure Insightful Interesting IT Java Lesson License Life Malware Management Mind Model MythTV Network NLP OpenSource Performance PHP PM Presentation Project Quote Remote Scrum Security SEO Social Spam Storage Stress Tactical Team Tech TED Time Management Tip Tool TV Ubuntu Unix/Linux Vids Vim Virtual VmWare Voip Web Wordpress

Archives

  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
rss Comments rss valid xhtml 1.1 design by jide licensed as Creative Commons Attribution