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

Monday, 2 June 2014

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

Unix - User Administration

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

Unix - User Administration

There are three types of accounts on a Unix system:
  1. Root account: This is also called superuser and would have complete and unfettered control of the system. A superuser can run any commands without any restriction. This user should be assumed as a system administrator.
  2. System accounts: System accounts are those needed for the operation of system-specific components for example mail accounts and the sshd accounts. These accounts are usually needed for some specific function on your system, and any modifications to them could adversely affect the system.
  3. User accounts: User accounts provide interactive access to the system for users and groups of users. General users are typically assigned to these accounts and usually have limited access to critical system files and directories.
Unix supports a concept of Group Account which logically groups a number of accounts. Every account would be a part of any group account. Unix groups plays important role in handling file permissions and process management.

Managing Users and Groups:

There are three main user administration files:
  1. /etc/passwd: Keeps user account and password information. This file holds the majority of information about accounts on the Unix system.
  2. /etc/shadow: Holds the encrypted password of the corresponding account. Not all the system support this file.
  3. /etc/group: This file contains the group information for each account.
  4. /etc/gshadow: This file contains secure group account information.
Check all the above files using cat command.
Following are commands available on the majority of Unix systems to create and manage accounts and groups:
CommandDescription
useraddAdds accounts to the system.
usermodModifies account attributes.
userdelDeletes accounts from the system.
groupaddAdds groups to the system.
groupmodModifies group attributes.
groupdelRemoves groups from the system.
Create a Group
You would need to create groups before creating any account otherwise you would have to use existing groups at your system. You would have all the groups listed in /etc/groups file.
All the default groups would be system account specific groups and it is not recommended to use them for ordinary accounts. So following is the syntax to create a new group account:
 groupadd [-g gid [-o]] [-r] [-f] groupname
Here is the detail of the parameters:
OptionDescription
-g GIDThe numerical value of the group's ID.
-oThis option permits to add group with non-unique GID
-rThis flag instructs groupadd to add a system account
-fThis option causes to just exit with success status if the specified group already exists. With -g, if specified GID already exists, other (unique) GID is chosen
groupnameActaul group name to be created.
If you do not specify any parameter then system would use default values.
Following example would create developers group with default values, which is very much acceptable for most of the administrators.
$ groupadd developers

Modify a Group:

To modify a group, use the groupmod syntax:
$ groupmod -n new_modified_group_name old_group_name
To change the developers_2 group name to developer, type:
$ groupmod -n developer developer_2
Here is how you would change the financial GID to 545:
$ groupmod -g 545 developer

Delete a Group:

To delete an existing group, all you need are the groupdel command and the group name. To delete the financial group, the command is:
$ groupdel developer
This removes only the group, not any files associated with that group. The files are still accessible by their owners.

Create an Account

Let us see how to create a new account on your Unix system. Following is the syntax to create a user's account:
useradd -d homedir -g groupname -m -s shell -u userid accountname
Here is the detail of the parameters:
OptionDescription
-d homedirSpecifies home directory for the account.
-g groupnameSpecifies a group account for this account.
-mCreates the home directory if it doesn't exist.
-s shellSpecifies the default shell for this account.
-u useridYou can specify a user id for this account.
accountnameActual account name to be created
If you do not specify any parameter then system would use default values. The useradd command modifies the /etc/passwd, /etc/shadow, and /etc/group files and creates a home directory.
Following is the example which would create an account mcmohd setting its home directory to/home/mcmohd and group as developers. This user would have Korn Shell assigned to it.
$ useradd -d /home/mcmohd -g developers -s /bin/ksh mcmohd
Before issuing above command, make sure you already have developers group created usinggroupadd command.
Once an account is created you can set its password using the passwd command as follows:
$ passwd mcmohd20
Changing password for user mcmohd20.
New UNIX password:
Retype new UNIX password:
passwd: all authentication tokens updated successfully.
When you type passwd accountname, it gives you option to change the password provided you are super user otherwise you would be able to change just your password using the same command but without specifying your account name.

Modify an Account:

The usermod command enables you to make changes to an existing account from the command line. It uses the same arguments as the useradd command, plus the -l argument, which allows you to change the account name.
For example, to change the account name mcmohd to mcmohd20 and to change home directory accordingly, you would need to issue following command:
$ usermod -d /home/mcmohd20 -m -l mcmohd mcmohd20

Delete an Account:

The userdel command can be used to delete an existing user. This is a very dangerous command if not used with caution.
There is only one argument or option available for the command: .r, for removing the account's home directory and mail file.
For example, to remove account mcmohd20, you would need to issue following command:
$ userdel -r mcmohd20
If you want to keep her home directory for backup purposes, omit the -r option. You can remove the home directory as needed at a later time.

Posted By MIrza Abdul Hannan11:39:00 pm

Unix - File System Basics

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

Unix - File System Basics

A file system is a logical collection of files on a partition or disk. A partition is a container for information and can span an entire hard drive if desired.
Your hard drive can have various partitions which usually contains only one file system, such as one file system housing the / file system or another containing the /home file system.
One file system per partition allows for the logical maintenance and management of differing file systems.
Everything in Unix is considered to be a file, including physical devices such as DVD-ROMs, USB devices, floppy drives, and so forth.

Directory Structure:

Unix uses a hierarchical file system structure, much like an upside-down tree, with root (/) at the base of the file system and all other directories spreading from there.
A UNIX filesystem is a collection of files and directories that has the following properties:
  • It has a root directory (/) that contains other files and directories.
  • Each file or directory is uniquely identified by its name, the directory in which it resides, and a unique identifier, typically called an inode.
  • By convention, the root directory has an inode number of 2 and the lost+found directory has an inode number of 3. Inode numbers 0 and 1 are not used. File inode numbers can be seen by specifying the -i option to ls command.
  • It is self contained. There are no dependencies between one filesystem and any other.
The directories have specific purposes and generally hold the same types of information for easily locating files. Following are the directories that exist on the major versions of Unix:
DirectoryDescription
/This is the root directory which should contain only the directories needed at the top level of the file structure.
/binThis is where the executable files are located. They are available to all user.
/devThese are device drivers.
/etcSupervisor directory commands, configuration files, disk configuration files, valid user lists, groups, ethernet, hosts, where to send critical messages.
/libContains shared library files and sometimes other kernel-related files.
/bootContains files for booting the system.
/homeContains the home directory for users and other accounts.
/mntUsed to mount other temporary file systems, such as cdrom and floppy for the CD-ROM drive and floppy diskette drive, respectively
/procContains all processes marked as a file by process number or other information that is dynamic to the system.
/tmpHolds temporary files used between system boots
/usrUsed for miscellaneous purposes, or can be used by many users. Includes administrative commands, shared files, library files, and others
/varTypically contains variable-length files such as log and print files and any other type of file that may contain a variable amount of data
/sbinContains binary (executable) files, usually for system administration. For examplefdisk and ifconfig utlities.
/kernelContains kernel files

Navigating the File System:

Now that you understand the basics of the file system, you can begin navigating to the files you need. The following are commands you'll use to navigate the system:
CommandDescription
cat filenameDisplays a filename.
cd dirnameMoves you to the directory identified.
cp file1 file2Copies one file/directory to specified location.
file filenameIdentifies the file type (binary, text, etc).
find filename dirFinds a file/directory.
head filenameShows the beginning of a file.
less filenameBrowses through a file from end or beginning.
ls dirnameShows the contents of the directory specified.
mkdir dirnameCreates the specified directory.
more filenameBrowses through a file from beginning to end.
mv file1 file2Moves the location of or renames a file/directory.
pwdShows the current directory the user is in.
rm filenameRemoves a file.
rmdir dirnameRemoves a directory.
tail filenameShows the end of a file.
touch filenameCreates a blank file or modifies an existing file.s attributes.
whereis filenameShows the location of a file.
which filenameShows the location of a file if it is in your PATH.
The df Command:
The first way to manage your partition space is with the df (disk free) command. The command df -k (disk free) displays the disk space usage in kilobytes, as shown below:
$df -k
Filesystem      1K-blocks      Used   Available Use% Mounted on
/dev/vzfs        10485760   7836644     2649116  75% /
/devices                0         0           0   0% /devices
$
Some of the directories, such as /devices, shows 0 in the kbytes, used, and avail columns as well as 0% for capacity. These are special (or virtual) file systems, and although they reside on the disk under /, by themselves they do not take up disk space.
The df -k output is generally the same on all Unix systems. Here's what it usually includes:
ColumnDescription
FilesystemThe physical file system name.
kbytesTotal kilobytes of space available on the storage medium.
usedTotal kilobytes of space used (by files).
availTotal kilobytes available for use.
capacityPercentage of total space used by files.
Mounted onWhat the file system is mounted on.
You can use the -h (human readable) option to display the output in a format that shows the size in easier-to-understand notation.

The du Command:

The du (disk usage) command enables you to specify directories to show disk space usage on a particular directory.
This command is helpful if you want to determine how much space a particular directory is taking. Following command would display number of blocks consumed by each directory. A single block may take either 512 Bytes or 1 Kilo Byte depending on your system.
$du /etc
10     /etc/cron.d
126    /etc/default
6      /etc/dfs
...
$
The -h option makes the output easier to comprehend:
$du -h /etc
5k    /etc/cron.d
63k   /etc/default
3k    /etc/dfs
...
$

Mounting the File System:

A file system must be mounted in order to be usable by the system. To see what is currently mounted (available for use) on your system, use this command:
$ mount
/dev/vzfs on / type reiserfs (rw,usrquota,grpquota)
proc on /proc type proc (rw,nodiratime)
devpts on /dev/pts type devpts (rw)
$
The /mnt directory, by Unix convention, is where temporary mounts (such as CD-ROM drives, remote network drives, and floppy drives) are located. If you need to mount a file system, you can use the mount command with the following syntax:
mount -t file_system_type device_to_mount directory_to_mount_to
For example, if you want to mount a CD-ROM to the directory /mnt/cdrom, for example, you can type:
$ mount -t iso9660 /dev/cdrom /mnt/cdrom
This assumes that your CD-ROM device is called /dev/cdrom and that you want to mount it to /mnt/cdrom. Refer to the mount man page for more specific information or type mount -h at the command line for help information.
After mounting, you can use the cd command to navigate the newly available file system through the mountpoint you just made.

Unmounting the File System:

To unmount (remove) the file system from your system, use the umount command by identifying the mountpoint or device
For example, to unmount cdrom, use the following command:
$ umount /dev/cdrom
The mount command enables you to access your file systems, but on most modern Unix systems, the automount function makes this process invisible to the user and requires no intervention.

User and Group Quotas:

User and group quotas provide the mechanisms by which the amount of space used by a single user or all users within a specific group can be limited to a value defined by the administrator.
Quotas operate around two limits that allow the user to take some action if the amount of space or number of disk blocks start to exceed the administrator defined limits:
  • Soft Limit: If the user exceeds the limit defined, there is a grace period that allows the user to free up some space.
  • Hard Limit: When the hard limit is reached, regardless of the grace period, no further files or blocks can be allocated.
There are a number of commands to administer quotas:
CommandDescription
quotaDisplays disk usage and limits for a user of group.
edquotaThis is a quota editor. Users or Groups quota can be edited using this command.
quotacheckScan a filesystem for disk usage, create, check and repair quota files
setquotaThis is also a command line quota editor.
quotaonThis announces to the system that disk quotas should be enabled on one or more filesystems.
quotaoffThis announces to the system that disk quotas should be disabled off one or more filesystems.
repquotaThis prints a summary of the disc usage and quotas for the specified file systems

Posted By MIrza Abdul Hannan11:36:00 pm