top of page
Writer's pictureblkrishnarao

Brush up on Unix Fundamentals Part - 04 | Unix Environment & Process | Unix 101

Updated: Apr 16

The following opinions expressed in this post are my own and in no way connected to my employer. 




Unix Environment and Unix Processes Management


Unix Environment

Unix environment is comprised of environment variables.

Variables are character strings which usually are assigned values like number/text/.. and when these variables are sourced with $<variable_name> ; $(dollar sign) followed by variable name they define the value that was assigned to them.


Eg:

export command helps to set the value in environment.

export NAME = 'myname'

echo command helps to view value of a variable, below would display 'myname' (without quotes) as output

echo $NAME

So suppose an path is assigned to a variable where all executable are located and we use the variable to run those executable irrespective of going to the path or giving full path.

export LOCATION = '/my/path/to/executable'

Let the executable be a shell script (eg: script.sh), so it can be executed like below

sh $LOCATION/script1.sh
sh $LOCATION/script2.sh

or 

cd $LOCATION


Unix Environment Initialisation 

When a user logs in the shell undergoes a 2 step process basically to set the environment for the user.

. /etc/profile 

and

. profile

First the shell checks if /etc/profile exists, if yes then reads it else skips.

Then shell checks for .profile in user's home directory, if yes read it else skips

Bash and ksh may have additional set of files. Custom environment files/variables can be added here.


/etc/profile is maintained by unix admin.

It usually sets shell information required by all users.

.profile is a user specific file, it sets additional set of shell information for the user.

This may include details on type of terminal/directory location variables/look and feel settings.


In an Unix environment when we try executing any command, by default unix would first try locating the executable file in same directory if not, it would  search for the path in environment variable $PATH.


PATH variable specifies locations where the shell would search for the file.

We can extensively set the path value to include our locations as well.

PATH=/bin;/usr/bin
export PATH=$PATH;/my/path/included/in/path

PS1 and PS2 Variables


PS1 variable is used to display text at your command prompt.

myname@hostname /my/home/directory$

The above command prompt display can be set using PS1 variable in your .profile file.

PS1 = "[\u@\h \w]\$"
\u -- user logged in
\h -- hostname of server
\w -- current working directory

Few other escape sequences are as below:

\t --  time in HH:MM:SS
\d -- date in weekday month date
\n -- new line
\s -- current shell environment
\# -- command number of each command

PS2 variable shows the waiting command line when we enter an incomplete command.

cat text.txt |grep     

This is an incomplete command and when pressed enter the command prompt would wait in next line for complete command.


This second prompt can be set using PS2

PS2 = "secondary prompt =>"

So, 

$ cat text.txt|grep

secondary prompt=> myname


This will end up as "cat text.txt|grep myname"


Some widely used environment variables are:

DISPLAY              -- contains indentifier for display
HOME                 -- value for current users home directory
LANG                 -- shows default system locale
LD_LIBRARY_PATH  		-- dynamic linker searches for directories to link to build a process
PWD                  -- current working directory
TERM                 -- refers to display type
TZ                   -- time zone

Unix Process Management


In Unix every program runs with its own environment independent of other programs running simultaneously.


PID (Process ID)

The five digit code that acts as an ID to identify a process is known as PID. It is an unique number for existing process.


Foreground Process

When we run an program(executable/script) on command prompt it waits for completion of program and you cannot run another program at same terminal unless this prompt comes out. This is a foreground process which runs in foreground.


Background Process

when a process runs without being connected to your keyboard. It only awaits if an input is expected from user.


The simplest way to start a background process is to affix '&' at the end.

Eg: cp text.txt /to/destination/directory &

List Running Processes:

ps             -- process status; shows all process
ps -f          -- full option; shows more information
Eg:            ps -f 
UID        PID    PPID   c    STIME    TTY     TIME       CMD
user1      6643    3005  0   11:12:02  pts/6   0:01   more text.txt
UID        -- user id who owns process
PID        -- Process id
PPID       -- Parent Process ID (if its a sub process)
c          -- CPU Utilisation of process
STIME      -- Process Start Time
TTY        -- Terminal Type associated with process
TIME       -- CPU time taken by process
CMD        -- command that started the process
ps -a         shows information about all users
ps -x         shows information without terminals
ps -u         shows full information as -f
ps -e         displays extended information

Stopping a Process


A foreground process can be killed by CTRL+c

A Background process can be killed using below 2 steps:

First, get the process id from (ps -ef)

Then, kill the process (kill -9 PID)


Parent And Child Process

Most commands that we execute have shell as their parent Id.


Zombie and Orphan Process

When a child request is killed, the parent process is informed via SIGCHLD signal.

When a parent request is killed the child process will attain the init process id as its parent id, this kind of child process becomes Orphan Process

When we use ps to list all process and we see any request as Z state, it is for Zombie process or Defunct Process.

A Zombie process is one which has completed execution but still has an entry in the process table.


Daemon Process

It is a background process that runs with root permission and waits for something that it is capable of working with. Daemon process usually cannot open a tty terminal hence any daemon process would have ? in the terminal filed. If a program needs to run for long time, it is beneficial to run it as daemon process.


TOP Command

Widely used command to monitor real time processes and their CPU/Memory usage in an Unix environment.


0 views0 comments

Recent Posts

See All

Comments


bottom of page