My Linux Notes
Please note, this is my linux cheat sheet I don't actively correct the errors on this page
# Here is a load balancing system for multicore systems
bash file
you also need this code (a dummy function)


# Starting and running a Bash script
A bash script starts with
#!/bin/bash

and if the filename is mybashtest it can be run in two ways
bash mybashtest
. mybashtest



# Reads from keyboard
read myid
echo $myid



# Defining Variables
myid=2
echo $myid


# Reads from keyboard
read myid
echo $myid



# Getting the number of CPUs
$ grep processor /proc/cpuinfo
$cat /proc/cpuinfo | grep processor | wc -l

NUMCPU=`cat /proc/cpuinfo | grep processor | wc -l | awk '{ print $1 } '`
echo $NUMCPU



# Checking permissions
$ le -l
$ chmod #helps you change that



# Job Queing
$ nice 					# runs commands with modified scheduling priority
$ atd  					# run jobs queued for later execution
$ ps -ef |grep java			# shows the processes with java in their name
$ free					# displayes free and used memory
two linux scheduling packages openpbs.org and gridengine.sunsource.net


# Running in the background
$ nohup java -cp . automatedAgentApplet.autoAgentTerminal &



# Load Balancing
shows the top two processes
ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -2

reflects the CPU load
USAGE=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{ print $1 } '`
echo $USAGE

PID=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $2 }'`
PNAME=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $3 }'`


Inform the admin if the first CPU is not busy enough (<40%)
USAGE=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk\
'{ print $1 } '`
USAGE=${USAGE%%.*}
[ $USAGE -lt 40 ] && mail -s "CPU is working in less than 40% capacity"

more: http://www.sandervanvugt.nl/nl/artikelen/linux-advanced-bash-scripting-managing-cpu-hogs




# Sending email after the process is finished
you need mailutils install that with sudo agp-get install mailutils
you also need to install postfix in order to use remote mail servers, it's easy
this command simply sends you an email
ifconfig | awk '{print $2}' | head -2 | tail -1 | sed 's/addr://' | mail -s "MY IP" mylinuxdesktop@gmail.com


echo "Please enter the process ID to monitor:"
read PID
while [ -e /proc/$PID ] ; do
sleep 600 # chage it to 30 or any other time in seconds (600 seconds=10 minutes!)
done
echo "Process done" | mail -s "process finished" someone@example.com



this little programm emails you whenever the top process is beeing finished
save it to a file on the server i.e "mailme"
run it in nohup: $nohup bash mailme&

PID=`ps -eo pcpu,pid -o comm= | sort -k1 -n -r | head -1 | awk '{print $2 }'`
while [ -e /proc/$PID ] ; do
sleep 600 # chage it to 30 or any other time in seconds (600 seconds=10 minutes!)
done
ifconfig | awk '{print $2}' | head -2 | tail -1 | sed 's/addr://' | mail -s "Process done" mylinuxdesktop@gmail.com




# I use the following terminal / bash commands extensively
collected from the Internet

break    Exit from a loop
cal      Display a calendar
cat      Display the contents of a file
cd       Change Directory
chgrp    Change group ownership
chmod    Change access permissions
chown    Change file owner and group
clear    Clear terminal screen
cmp      Compare two files
comm     Compare two sorted files line by line
cp       Copy one or more files to another location
cron     Daemon to execute scheduled commands
df       Display free disk space
diff     Display the differences between two files
diff3    Show differences among three files
dircolors Colour setup for `ls'
echo     Display message on screen
egrep    Search file(s) for lines that match an extended expression
eject    Eject removable media
expr     Evaluate expressions
eval     Evaluate several commands/arguments
find     Search for files that meet a desired criteria
free     Display memory usage
grep     Search file(s) for lines that match a given pattern
ifconfig Configure a network interface
kill     Stop a process from running
locate   Find files
ls       List information about file(s)
man      Help manual
mkdir    Create new folder(s)
more     Display output one screen at a time
netstat  Networking information
nice     Set the priority of a command or job
nohup    Run a command immune to hangups
passwd   Modify a user password
ps       Process status
read     read a line from standard input
rsync    Remote file copy (Synchronize file trees)
split    Split a file into fixed-size pieces
su       Substitute user identity
sudo     Execute a command as another user
top      List processes running on the system
traceroute Trace Route to Host
vi       Text Editor
watch    Execute/display a program periodically
whoami   Print the current user id and name (`id -un')
Wget     Retrieve web pages or files via HTTP, HTTPS or FTP
.        Run a command script in the current shell
###      Comment / Remark

Home | last update Aug 25, 2008