Environment Variables
Unix Utilities
Process Management
Environment
An important Unix concept is the
environment, which is defined by
environment variables.
A shell environment variable is a
shell setting that can be viewed and
adjusted at any time.
Some are set by the system, others
by you, yet others by the shell, or
any program that loads another
program.
Variable
A variable is a character string to which we
assign a value. The value assigned could be a
number, text, filename, device, or any other
type of data.
For example, first we set a variables TEST and
then we access its value using
echocommand:
$TEST="Unix Programming"
$echo $TEST
Unix Programming
Shell Initialization
When you login to the system, the shell
undergoes a phase called initialization
to set up various environment. This is
usually a two step process that involves
the shell reading the following files
/etc/profile
.profile
The process is as follows
The shell checks to see whether the file /etc/profile exists.
If it exists, the shell reads it. Otherwise, this file is skipped. No error
message is displayed.
The shell checks to see whether the file .profile exists in your home
directory. Your home directory is the directory that you start out in
after you log in.
If it exists, the shell reads it; otherwise, the shell skips it. No error
message is displayed. As soon as both of these files have been read,
the shell displays a prompt $ (This is the prompt where you can
enter commands in order to have them execute.)
The .profile File
The file /etc/profile is maintained by the system administrator of
your UNIX machine and contains shell initialization information
required by all users on a system.
The file .profile is under your control. You can add as much shell
customization information as you want to this file. The minimum
set of information that you need to configure includes
The type of terminal you are using
A list of directories in which to locate commands
A list of variables effecting look and feel of your terminal.
Environment Setup(setting the
PATH)
When you type any command on
command prompt, the shell has to
locate the command before it can be
executed.
The PATH variable specifies the locations
in which the shell should look for
commands. Usually it is set as follows
$PATH=/bin:/usr/bin
If you request the shell to execute a
command and it cannot find it in any
of the directories given in the PATH
variable, a message similar to the
following appears
$hello
hello: not found
$
PS1 and PS2 Variables
PS1 - to set the prompt string, ofcourse!
PS2 - to set the subshell prompt string, which by default
is ">",
when we enter a command half and press enter, we get
the subshell prompt string which is >, then we can
complete the command and press enter, it runs. We can
change the > to something by modifying this variable.eg.
$ echo "this is a
> test"
this is a
test
$
PS1 and PS2 Variables
The characters that the shell displays as your command prompt
are stored in the variable PS1. You can change this variable to be
anything you want. As soon as you change it, it'll be used by the
shell from that point on.
For example, if you issued the command
$PS1='=>'
=>
=>
=>
Your prompt would become =>. To set the value of PS1 so that it
shows the working directory, issue the command
=>PS1="[\u@\h \w]\$"
Set command is used to show all the
environment variable that has been
set.
Syntax: $ set
Unix Utilities
Before you print a file on a UNIX
system, you may want to reformat it
to adjust the margins, highlight some
words, and so on.
The pr Command
Theprcommand does minor formatting of
files on the terminal screen or for a printer.
For example, if you have a long list of
names in a file, you can format it onscreen
into two or more columns.
Here is the syntax ofprcommand
pr option(s) filename(s)
Theprchanges the format of the file only
on the screen or on the printed copy; it
doesn't modify the original file.
pr Command options
$cat food
Sweet Tooth
Bangkok Wok
Mandalay
Afghani Cuisine
Isle of Java
Big Apple Deli
Sushi and Sashimi
Tio Pepe's Peppers
........
$
The lp and lpr Commands
The commandlporlprprints a file onto
paper as opposed to the screen display.
Once you are ready with formatting
usingpr command, you can use any of
these commands to print your file on
printer connected with your computer.
$lp food
request id is laserp-525 (1 file)
$
The lp command shows an ID that you can use to
cancel the print job or check its status.
If you are using lp command, you can use
-nNumoption to print Num number of copies.
Along with the command lpr, you can use-Numfor
the same.
If there are multiple printers connected with the
shared network, then you can choose a printer
using dprinter option along with lp command and
for the same purpose you can use -Pprinteroption
along with lpr command. Here printer is the printer
name.
The lpstat and lpq Commands
The lpstat command shows what's in the printer
queue: request IDs, owners, file sizes, when the jobs
were sent for printing, and the status of the requests.
Use lpstat -o if you want to see all output requests
rather than just your own. Requests are shown in the
order they'll be printed
$lpstat -o
laserp-573 john 128865 Nov 7 11:27 on laserp
laserp-574 grace 82744 Nov 7 11:28
laserp-575 john 23347 Nov 7 11:35
$
Thelpqgives slightly different information than
lpstat -o
$lpq
laserp is ready and printing
Rank Owner
Job Files
Total Size
active john
573 report.ps
128865 bytes
1st grace
574 ch03.ps ch04.ps
82744
bytes
2nd john
575 standard input
23347 bytes
$
The cancel and lprm Commands
The cancel terminates a printing request from the lp command.
The lprm terminates lpr requests. You can specify either the ID of
the request (displayed by lp or lpq) or the name of the printer.
$cancel laserp-575
request "laserp-575" cancelled
$
To cancel whatever request is currently printing, regardless of its
ID, simply enter cancel and the printer name
$cancel laserp
request "laserp-573" cancelled
$
The lprm command will cancel the active job if it
belongs to you. Otherwise, you can give job
numbers as arguments, or use a dash (-) to
remove all of your jobs
$lprm 575
dfA575diamond dequeued
cfA575diamond dequeued
$
The lprm command tells you the actual filenames
removed from the printer queue.
Sending Email
You use the Unix mail command to
send and receive mail. Here is the
syntax to send an email
$mail [-s subject] [-c cc-addr] [-b bccaddr] to-addr
Here are important options related to
mail command:
Options Related To Mail Command:
Following is the example to send a text message to
[email protected].
$mail -s "Text Message"
[email protected] You are then expected to type in your message, followed by
an "control-D" at the beginning of a line. To stop simply type
dot (.) as follows
Hi,
This is a test
.
Cc:
You can send a complete file using a
redirect < operator as follows
$mail -s "Report 05/06/07
[email protected] < demo.txt
To check incoming email at your
Unix system you simply type email
as follows
$mail
no email
Unix - Processes Management
When you execute a program on your UNIX system, the
system creates a special environment for that program. This
environment contains everything needed for the system to
run the program as if no other program were running on the
system.
Whenever you issue a command in UNIX, it creates, or starts,
a new process. When you tried out thelscommand to list
directory contents, you started a process. A process, in
simple terms, is an instance of a running program.
Pids eventually repeat because all the possible numbers are
used up and the next pid rolls or starts over. At any one time,
no two processes with the same pid exist in the system
because it is the pid that UNIX uses to track each process.
Starting a Process
When you start a process (run a
command), there are two ways you
can run it
Foreground Processes
Background Processes
Foreground Processes
By default, every process that you start runs in the foreground. It
gets its input from the keyboard and sends its output to the screen.
You can see this happen with the ls command. If I want to list all the
files in my current directory, I can use the following command
$ls ch*.doc
This would display all the files whose name start with ch and ends
with .doc
ch01-1.doc ch010.doc ch02.doc
ch04-1.doc ch040.doc ch05.doc
ch01-2.doc ch02-1.doc
ch03-2.doc
ch06-2.doc
The process runs in the foreground, the
output is directed to my screen, and if the
ls command wants any input (which it does
not), it waits for it from the keyboard.
While a program is running in foreground
and taking much time, we cannot run any
other commands (start any other
processes) because prompt would not be
available until program finishes its
processing and comes out.
Background Processes
A background process runs without
being connected to your keyboard. If
the background process requires any
keyboard input, it waits.
The advantage of running a process in
the background is that you can run
other commands; you do not have to
wait until it completes to start another!
The simplest way to start a background process is
to add an ampersand ( &) at the end of the
command.
$ls ch*.doc &
This would also display all the files whose name
start with ch and ends with .doc
ch01-1.doc ch010.doc ch02.doc
ch04-1.doc ch040.doc ch05.doc
ch01-2.doc ch02-1.doc
ch03-2.doc
ch06-2.doc
Here if the ls command wants any input (which it does not), it
goes into a stop state until I move it into the foreground and give
it the data from the keyboard.
That first line contains information about the background process
- the job number and process ID. You need to know the job
number to manipulate it between background and foreground.
If you press the Enter key now, you see the following
[1] + Done
ls ch*.doc &
$
The first line tells you that the ls command background process
finishes successfully. The second is a prompt for another command.
Listing Running Processes
It is easy to see your own processes
by running the ps (process status)
command as follows
$ps
PID
TTY
TIME
CMD
18358
ttyp3 00:00:00 sh
18361
ttyp3 00:01:31 abiword
18789
ttyp3 00:00:00 ps
One of the most commonly used flags for ps is
the -f ( f for full) option, which provides more
information as shown in the following example
$ps -f
UID
PID PPID C STIME TTY
amrood 6738 3662 0 10:23:03
amrood 6739 3662 0 10:22:54
amrood 3662 3657 0 08:10:53
amrood 6892 3662 4 10:51:50
TIME CMD
pts/6 0:00 first_one
pts/6 0:00 second_one
pts/6 0:00 -ksh
pts/6 0:00 ps -f
Stopping Processes
Ending a process can be done in several
different ways. Often, from a console-based
command, sending a CTRL + C keystroke (the
default interrupt character) will exit the
command. This works when process is running
in foreground mode.
If a process is running in background mode
then first you would need to get its Job ID using
ps command and after that you can use kill
command to kill the process as follows
$ps -f
UID
PID PPID C STIME TTY
amrood 6738 3662 0 10:23:03
first_one
amrood 6739 3662 0 10:22:54
second_one
amrood 3662 3657 0 08:10:53
amrood 6892 3662 4 10:51:50
$kill 6738
Terminated
TIME CMD
pts/6 0:00
pts/6 0:00
pts/6 0:00 -ksh
pts/6 0:00 ps -f
Here kill command would terminate
first_one process. If a process ignores
a regular kill command, you can use
kill -9 followed by the process ID as
follows
$kill -9 6738
Terminated
Parent and Child Processes
Each unix process has two ID numbers
assigned to it: Process ID (pid) and Parent
process ID (ppid). Each user process in
the system has a parent process.
Most of the commands that you run have
the shell as their parent. Check ps -f
example where this command listed both
process ID and parent process ID.
Zombie and Orphan Processes
Normally, when a child process is killed, the parent process
is told via a SIGCHLD signal. Then the parent can do some
other task or restart a new child as needed. However,
sometimes the parent process is killed before its child is
killed. In this case, the "parent of all
processes,"initprocess, becomes the new PPID (parent
process ID). Sometime these processes are called orphan
process.
When a process is killed, a ps listing may still show the
process with a Z state. This is a zombie, or defunct, process.
The process is dead and not being used. These processes
are different from orphan processes.They are the processes
that has completed execution but still has an entry in the
process table.
Daemon Processes
Daemons are system-related
background processes that often run
with the permissions of root and
services requests from other processes.
A daemon process has no controlling
terminal. It cannot open /dev/tty. If you
do a "ps -ef" and look at the tty field, all
daemons will have a ? for the tty.
More clearly, a daemon is just a
process that runs in the background,
usually waiting for something to
happen that it is capable of working
with, like a printer daemon is waiting
for print commands.
If you have a program which needs
to do long processing then its worth
to make it a daemon and run it in
background.
The top Command
The top command is a very useful tool for quickly
showing processes sorted by various criteria.
It is an interactive diagnostic tool that updates
frequently and shows information about physical and
virtual memory, CPU usage, load averages, and your
busy processes.
Here is simple syntax to run top command and to see
the statistics of CPU utilization by different processes
$top
Job ID Versus Process ID
Background and suspended processes are
usually manipulated via job number (job ID).
This number is different from the process ID
and is used because it is shorter.
In addition, a job can consist of multiple
processes running in series or at the same
time, in parallel, so using the job ID is easier
than tracking the individual processes.