You are on page 1of 2

UNIT II ESSENTIAL LINUX COMMANDS

Processes in Linux & Process Fundamentals file, processes it and sends the output to the
Linux is a multiprocessing/multi-user OS, standard output file. Linux provides various filters
processes are separate tasks with their own right like cat, grep, wc, tr cut etc to enable you to work
and responsibilities. A program in execution is effectively with data.
called process. All processes communicate with Redirecting input/output
each other and with the kernel to co ordinate In all OS there is a standard input device and a
their activities. Linux supports a large number of standard output device. In Unix also standard
Inter Process Communication mechanisms. (IPC) input device is keyboard and standard output
Scheduler: There are several processes running in device is monitor (display screen). The standard
memory at any given moment, the CPU error device is also the display screen.
can cater to only one of these processes, as others
should wait their turn. There is a program called
scheduler running in memory deciding which
process should get the cpu attention and when.
Linux process states: At any given moment a
process in memory can be in any one of the Unix and Linux allow you to change the
following states: standard input and output temporarily by using
1) Submit state: When you execute a program the redirection and piping. Redirection is the process
scheduler submits your process to a queue of changing the assignment of standard input
called process queue. At this instant the process is device, standard output device, and standard error
said to be in submit state. device. There are three types of redirection: Input
2) Hold state: Once submitted the process waits its redirection, Output redirection, Error redirection.
turn in the queue for sometime. This is the Sometimes it is useful to redirect the input or
hold state. output to a file or printer. You may have to
3) Ready state: As the process advances in the redirect a directory listing to a file. Unix and
queue at some instant it would become the Linux provide redirection symbols for this
next one in the queue to receive CPU attention. purpose:
This is the ready state. The symbol > implies redirection of output. It
4) Run state: Finally the process gets the attention sends the output of a command to a file or a
of cpu and starts execution there by attains device like printer.
the run state. The symbol < implies redirection of input. It takes
5) Wait state: some processes might be required the input needed for a command from a
to do an I/O operation. Since I/O is a slow printer or file rather than from the keyboard.
operation the cpu cant lie idle till the time I/O is The symbol >> adds output from a command
over. So such processes are put in wait state until to the end of a file without deleting the
their I/O is over and are then placed in the ready information already in the file.
state. Background Processing
6) Complete state: A process whose execution Most of the system processes run in the
comes to an end goes into complete state and is background, while the user executes their
then removed from the process queue. (stopped) processes in the foreground. If the user so desires
The process can be stopped by sending a signal. even he can run his processes in the background.
Connecting processes with pipes The user can run time-consuming tasks like
Redirection helps to commands to files. Piping sorting a huge file and storing the sorted output
facility lets us connect commands to other in a file in the background. This way he would not
commands and operations. To send the output of be required to wait till the sorting is over to be
one command as input to another command, the able to run the next process. He can
two commands must be joined using a pipe symbol immediately concentrate on another task the
(|) It is possible to join commands using a pipe moment sorting process is submitted to run in the
since many commands accept input from standard background.
input and send output to standard output. Such To run a process in the background, UNIX
commands are known as Filters. A filter is a provides the ampersand (&) symbol.While
program that takes input from the standard input executing a command, if this symbol is placed at
the end of the command then the command will be $ nohup sort employee.dat > output.emp
executed in the; background. When you run a Now we can safely log out (without any
process in the background a number is displayed hang-ups) without our process getting terminated
on the screen. This number is the PID of the on logging out. The output would be ready in
process that you have executed in the output.emp which can be verified when we log in
background. next time. If we do not redirect the output of
our background process the command acts
intelligently and stores the output in the file
‗nohup.out‘.
$ nohup sort employee.dat &
Scheduling Of Processes 16779
Linux can schedule processes to get Sending output to nohup.out
executed within next few-seconds to next few The ‗nohup.out‟ file is always created in
years. Once the user-has submitted a process to the current directory.
'Linux directing it to execute the process at a If the nohup command is used in a pipeline
specified time and date in future, there care should be taken to precede every command in
onwards Linux takes over. Linux manages to the pipeline by word nohup as shown below:
remember the processes to be executed and goes $ nohup cat employee.dat | nohup grep
about executing them whenever the time arises ‗Nagpur‘ | sort > address &
without needing any further directions from 12695
the user. That's the philosophy of scheduling Here all processes in the pipeline remain
processes. Execute the following command: alive when user logout. If not, command which is
$ ps -e | grep cron not preceded by nohup will die when you log out.
$ sort employee.dat > emp.out & If one of the processes in the pipeline dies the
17653 entire pipeline will collapse.
PSTREE COMMAND
$
147 ? 0:01 cron The pstree command shows the
Here the process cron stands for processes and parent – child relationship. It
chronograph. This system process is displays the
responsible for scheduling the other processes. running process in tree format.
Neither user nor super user can execute the kill command
executable file (/etc/cron) of this process directly. Some of the reasons why you would like
During booting Linux executes this file and to terminate a process in the middle of its
displays the message 'cron started' on the host execution are given below:
terminal. Once Linux launches this process there a) The terminal has hung. A typical e.g.: of
onwards cron is activated once every minute. this is when you attempt to cat a directory file.
When cron wakes up it checks whether any b) The program, which is running, has
scheduled job is available for it to execute. If it is, gone in an indefinite loop and hence is not
it executes the job and goes back to sleep again, getting terminated.
only to wake up the next minute to once again c) The system performance has gone
carry out the check. This cycle goes on till the below acceptable limits because of too many
Linux system isn't shut down on the host machine. processes running in the background. To carry out
There are three commands which make use of the killing a process first note the PID of the
cron daemon's scheduling capability. These are at, process to be killed using the ps command and
batch and crontab. use kill command.
The nohup command
To ensure that the processes that we have
executed should not die even when we log out, use
the nohup command. Using this command we can
submit the time consuming commands in the
background, log out and leave the terminal and
come next day to see our output ready.
Appropriately, nohup stands for no hang
ups. The nohup command's usage is shown below:

You might also like