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
ps -eo pcpu,pid,user,args | sort -r -k1 | less
%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.
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:
(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
Save, and enjoy the action!
(Note: use “sudo -K” to clear your sudo session)
I’ve added the “force” options, as I needed it for the project I’m working on.
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"
echo "Version 0.1"
if [ "$VERBOSE" = "y" ] ; then
if [ "$SETLOCK" = "y" ] ; then
verbose "set lock"
echo $$ >> $LOCK
verbose "Clear lock"
rm $LOCK >/dev/null 2>&1
pids=`cat $LOCK 2>/dev/null`
for pid in $pids
verbose "Kill $1 on process $pid"
kill $1 $pid >/dev/null 2>&1
verbose "Running command"
eval bash -c "$CMD"
verbose "Command done - exit value = $RET"
verbose "No waiting"
verbose "Waiting nice"
while [ -f "$LOCK" ] ; do
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;;
if [ $# -lt 2 ] ; then
verbose "Not enough parameters given"
while [ $APTS -lt $OPTS ] ; do
if [ -z "$LOCK" ] ; then
verbose "No lockfile given"
if [ -z "$CMD" ] ; then
verbose "No command given"
if [ -f "$LOCK" ] ; then
if [ "$DELAY" = "y" ] ; then
case "$FORCE" in
*) exit 99;;
Feel free to use it, yet I’m publishing it here under the GPL license
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 "
# usage of a control structure with non-wait locking
# this is done to avoid locking issues ^^
while [ "$var0" -lt "$LIMIT" ]
var0=`expr $var0 + 1`
eval $SUME "$SETLOCK -n $TMPDIR/lock_$ID $EXECUTE $PARAMETERS 2>/dev/null " 2>/dev/null
if [ $EC -eq 0 ] ; then
var0=`expr $LIMIT + 1`
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.
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
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.
When setting/changing variables within the scope of a subshell. And then attempt to use those variables outside the scope of this subshell won’t give the ‘expected result’.
A subshell has to be seen as a totally seperate process with it’s own environment.
(check a previous entry about processes)
Now the above might seem logical: Subshell, seperate process, … okay. Let’s say you want to pipe the output of a certain command thru a loop. In this loop you can manipulate/filter/… the data. In some cases you might want to set a flag if a certain criteria is met. Here is where it all comes into play. When you pipe an output to any kind of loop, it’ll become a subshell.
So as you might have guessed. The flag you wanted to set won’t cross it’s subshell borders. So you won’t be able to see it outside the loop. And yet again, an output that you might not have expected.
More info about “bash gotchas” can be found here