Showing posts with label Unix Tutorial. Show all posts
Showing posts with label Unix Tutorial. Show all posts

Monday, 2 June 2014

Unix - Useful Commands

Post By; Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

Unix - Useful Commands

This quick guide lists commands, including a syntax and brief description. For more detail, use:
$man command

Files and Directories:

These commands allow you to create directories and handle files.
CommandDescription
catDisplay File Contents
cdChanges Directory to dirname
chgrpchange file group
chmodChanging Permissions
cpCopy source file into destination
fileDetermine file type
findFind files
grepSearch files for regular expressions.
headDisplay first few lines of a file
lnCreate softlink on oldname
lsDisplay information about file type.
mkdirCreate a new directory dirname
moreDisplay data in paginated form.
mvMove (Rename) a oldname to newname.
pwdPrint current working directory.
rmRemove (Delete) filename
rmdirDelete an existing directory provided it is empty.
tailPrints last few lines in a file.
touchUpdate access and modification time of a file.

Manipulating data:

The contents of files can be compared and altered with the following commands.
CommandDescription
awkPattern scanning and processing language
cmpCompare the contents of two files
commCompare sorted data
cutCut out selected fields of each line of a file
diffDifferential file comparator
expandExpand tabs to spaces
joinJoin files on some common field
perlData manipulation language
sedStream text editor
sortSort file data
splitSplit file into smaller files
trTranslate characters
uniqReport repeated lines in a file
wcCount words, lines, and characters
viOpens vi text editor
vimOpens vim text editor
fmtSimple text formatter
spellCheck text for spelling error
ispellCheck text for spelling error
ispellCheck text for spelling error
emacsGNU project Emacs
ex, editLine editor
emacsGNU project Emacs
emacsGNU project Emacs

Compressed Files:

Files may be compressed to save space. Compressed files can be created and examined:
CommandDescription
compressCompress files
gunzipUncompress gzipped files
gzipGNU alternative compression method
uncompressUncompress files
unzipList, test and extract compressed files in a ZIP archive
zcatCat a compressed file
zcmpCompare compressed files
zdiffCompare compressed files
zmoreFile perusal filter for crt viewing of compressed text

Getting Information:

Various Unix manuals and documentation are available on-line. The following Shell commands give information:
CommandDescription
aproposLocate commands by keyword lookup
infoDisplays command information pages online
manDisplays manual pages online
whatisSearch the whatis database for complete words.
yelpGNOME help viewer

Network Communication:

These following commands are used to send and receive files from a local UNIX hosts to the remote host around the world.
CommandDescription
ftpFile transfer program
rcpRemote file copy
rloginRemote login to a UNIX host
rshRemote shell
tftpTrivial file transfer program
telnetMake terminal connection to another host
sshSecure shell terminal or command connection
scpSecure shell remote file copy
sftpsecure shell file transfer program
Some of these commands may be restricted at your computer for security reasons.

Messages between Users:

The UNIX systems support on-screen messages to other users and world-wide electronic mail:
CommandDescription
evolutionGUI mail handling tool on Linux
mailSimple send or read mail program
mesgPermit or deny messages
parcelSend files to another user
pineVdu-based mail utility
talkTalk to another user
writeWrite message to another user

Programming Utilities:

The following programming tools and languages are available based on what you have installed on your Unix.
CommandDescription
dbxSun debugger
gdbGNU debugger
makeMaintain program groups and compile programs.
nmPrint program's name list
sizePrint program's sizes
stripRemove symbol table and relocation bits
cbC program beautifier
ccANSI C compiler for Suns SPARC systems
ctraceC program debugger
gccGNU ANSI C Compiler
indentIndent and format C program source
bcInteractive arithmetic language processor
gclGNU Common Lisp
perlGeneral purpose language
phpWeb page embedded language
pyPython language interpreter
aspWeb page embedded language
CCC++ compiler for Suns SPARC systems
g++GNU C++ Compiler
javacJAVA compiler
appletvieweirJAVA applet viewer
netbeansJava integrated development environment on Linux
sqlplusRun the Oracle SQL interpreter
sqlldrRun the Oracle SQL data loader
mysqlRun the mysql SQL interpreter

Misc Commands:

These commands list or alter information about the system:
CommandDescription
chfnChange your finger information
chgrpChange the group ownership of a file
chownChange owner
datePrint the date
determinAutomatically find terminal type
duPrint amount of disk usage
echoEcho arguments to the standard options
exitQuit the system
fingerPrint information about logged-in users
groupaddCreate a user group
groupsShow group memberships
homequotaShow quota and file usage
iostatReport I/O statistics
killSend a signal to a process
lastShow last logins of users
logoutlog off UNIX
lunList user names or login ID
netstatShow network status
passwdChange user password
passwdChange your login password
printenvDisplay value of a shell variable
psDisplay the status of current processes
psPrint process status statistics
quota -vDisplay disk usage and limits
resetReset terminal mode
scriptKeep script of terminal session
scriptSave the output of a command or process
setenvSet environment variables
sttySet terminal options
timeTime a command
topDisplay all system processes
tsetSet terminal mode
ttyPrint current terminal name
umaskShow the permissions that are given to view files by default
unameDisplay name of the current system
uptimeGet the system up time
useraddCreate a user account
usersPrint names of logged in users
vmstatReport virtual memory statistics
wShow what logged in users are doing
whoList logged in users

Posted By MIrza Abdul Hannan11:45:00 pm

Unix - Signals and Traps

Post By; Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

Unix - Signals and Traps

Signals are software interrupts sent to a program to indicate that an important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control.
The following are some of the more common signals you might encounter and want to use in your programs:
Signal NameSignal NumberDescription
SIGHUP1Hang up detected on controlling terminal or death of controlling process
SIGINT2Issued if the user sends an interrupt signal (Ctrl + C).
SIGQUIT3Issued if the user sends a quit signal (Ctrl + D).
SIGFPE8Issued if an illegal mathematical operation is attempted
SIGKILL9If a process gets this signal it must quit immediately and will not perform any clean-up operations
SIGALRM14Alarm Clock signal (used for timers)
SIGTERM15Software termination signal (sent by kill by default).

List of Signals:

There is an easy way to list down all the signals supported by your system. Just issue kill -l command and it would display all the supported signals:
$ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL
 5) SIGTRAP      6) SIGABRT      7) SIGBUS       8) SIGFPE
 9) SIGKILL     10) SIGUSR1     11) SIGSEGV     12) SIGUSR2
13) SIGPIPE     14) SIGALRM     15) SIGTERM     16) SIGSTKFLT
17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU
25) SIGXFSZ     26) SIGVTALRM   27) SIGPROF     28) SIGWINCH
29) SIGIO       30) SIGPWR      31) SIGSYS      34) SIGRTMIN
35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3  38) SIGRTMIN+4
39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12
47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14
51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10
55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7  58) SIGRTMAX-6
59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX
The actual list of signals varies between Solaris, HP-UX, and Linux.

Default Actions:

Every signal has a default action associated with it. The default action for a signal is the action that a script or program performs when it receives a signal.
Some of the possible default actions are:
  • Terminate the process.
  • Ignore the signal.
  • Dump core. This creates a file called core containing the memory image of the process when it received the signal.
  • Stop the process.
  • Continue a stopped process.

Sending Signals:

There are several methods of delivering signals to a program or script. One of the most common is for a user to type CONTROL-C or the INTERRUPT key while a script is executing.
When you press the Ctrl+C key a SIGINT is sent to the script and as per defined default action script terminates.
The other common method for delivering signals is to use the kill command whose syntax is as follows:
$ kill -signal pid
Here signal is either the number or name of the signal to deliver and pid is the process ID that the signal should be sent to. For Example:
$ kill -1 1001
Sends the HUP or hang-up signal to the program that is running with process ID 1001. To send a kill signal to the same process use the folloing command:
$ kill -9 1001
This would kill the process running with process ID 1001.

Trapping Signals:

When you press the Ctrl+C or Break key at your terminal during execution of a shell program, normally that program is immediately terminated, and your command prompt returned. This may not always be desirable. For instance, you may end up leaving a bunch of temporary files that won't get cleaned up.
Trapping these signals is quite easy, and the trap command has the following syntax:
$ trap commands signals
Here command can be any valid Unix command, or even a user-defined function, and signal can be a list of any number of signals you want to trap.
There are three common uses for trap in shell scripts:
  1. Clean up temporary files
  2. Ignore signals

Cleaning Up Temporary Files:

As an example of the trap command, the following shows how you can remove some files and then exit if someone tries to abort the program from the terminal:
$ trap "rm -f $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 2
From the point in the shell program that this trap is executed, the two files work1$$ and dataout$$ will be automatically removed if signal number 2 is received by the program.
So if the user interrupts execution of the program after this trap is executed, you can be assured that these two files will be cleaned up. The exit command that follows the rm is necessary because without it execution would continue in the program at the point that it left off when the signal was received.
Signal number 1 is generated for hangup: Either someone intentionally hangs up the line or the line gets accidentally disconnected.
You can modify the preceding trap to also remove the two specified files in this case by adding signal number 1 to the list of signals:
$ trap "rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit" 1 2
Now these files will be removed if the line gets hung up or if the Ctrl+C key gets pressed.
The commands specified to trap must be enclosed in quotes if they contain more than one command. Also note that the shell scans the command line at the time that the trap command gets executed and also again when one of the listed signals is received.
So in the preceding example, the value of WORKDIR and $$ will be substituted at the time that the trap command is executed. If you wanted this substitution to occur at the time that either signal 1 or 2 was received you can put the commands inside single quotes:
$ trap 'rm $WORKDIR/work1$$ $WORKDIR/dataout$$; exit' 1 2

Ignoring Signals:

If the command listed for trap is null, the specified signal will be ignored when received. For example, the command:
$ trap '' 2
Specifies that the interrupt signal is to be ignored. You might want to ignore certain signals when performing some operation that you don't want interrupted. You can specify multiple signals to be ignored as follows:
$ trap '' 1 2 3 15
Note that the first argument must be specified for a signal to be ignored and is not equivalent to writing the following, which has a separate meaning of its own:
$ trap  2
If you ignore a signal, all subshells also ignore that signal. However, if you specify an action to be taken on receipt of a signal, all subshells will still take the default action on receipt of that signal.

Resetting Traps:

After you've changed the default action to be taken on receipt of a signal, you can change it back again with trap if you simply omit the first argument; so
$ trap 1 2
resets the action to be taken on receipt of signals 1 or 2 back to the default.

Posted By MIrza Abdul Hannan11:43:00 pm

Unix - System Performance

Post By; Hanan Mannan
Contact Number: Pak (+92)-321-59-95-634
-------------------------------------------------------

Unix - System Performance

The purpose of this tutorial is to introduce the performance analyst to some of the free tools available to monitor and manage performance on UNIX systems, and to provide a guideline on how to diagnose and fix performance problems in Unix environment.
UNIX has following major resource types that need to be monitored and tuned:
  • CPU
  • Memory
  • Disk space
  • Communications lines
  • I/O Time
  • Network Time
  • Applications programs

Performance Components:

There are following major five component where total system time goes:
ComponentDescription
User state CPUThe actual amount of time the CPU spends running the users program in the user state. It includes time spent executing library calls, but does not include time spent in the kernel on its behalf.
System state CPUThis is the amount of time the CPU spends in the system state on behalf of this program. All I/O routines require kernel services. The programmer can affect this value by the use of blocking for I/O transfers.
I/O Time and Network TimeThese are the amount of time spent moving data and servicing I/O requests
Virtual Memory PerformanceThis includes context switching and swapping.
Application ProgramTime spent running other programs - when the system is not servicing this application because another application currently has the CPU.

Performance Tools:

Unix provides following important tools to measure and fine tune Unix system performance:
CommandDescription
nice/reniceRun a program with modified scheduling priority
netstatPrint network connections, routing tables, interface statistics, masquerade connections, and multicast memberships
timeTime a simple command or give resource usage
uptimeSystem Load Average
psReport a snapshot of the current processes.
vmstatReport virtual memory statistics
gprofDisplay call graph profile data
profProcess Profiling
topDisplay system tasks

Posted By MIrza Abdul Hannan11:40:00 pm