Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
22 views

Commands and Process

Commands and Process ....
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Commands and Process

Commands and Process ....
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 58

2.

Commands & Processes


What is a Command?
• Command is a specific order from a user to the
computer's operating system or to an application to perform a
service.
• A command is an instruction given by a user telling
a computer to do something, such a run a single program or a
group of linked programs. Commands are generally issued by
typing them in at the command line
• Operating systems such as DOS that do not have a graphical
user interface (GUI) offer a simple command line interface in
which you type the command on a designated line in
a display panel.
• The part of the operating system that handles commands is
usually called a command interpreter or a shell.
Meaning of Internal and external commands

• Two types – Internal and External


• Internal commands :
– Commands which are built into the shell.
– are resident in the main kernel file: command.com(or
cmd.exe)
– For all the shell built-in commands, execution of the
same is fast in the sense that the shell doesn’t have to
search the given path for them in the PATH variable, and
also no process needs to be spawned for executing it.
Examples: source, cd, fg, etc.
– For example, when using the "cd" command, no process is
created. The current directory simply gets changed on executing
it.

• External Commands:
– Commands which aren’t built into the shell.
– When an external command has to be executed, the shell looks
for its path given in the PATH variable, and also a new process
has to be spawned and the command gets executed.
– They are usually located in /bin or /usr/bin.
– For example, when you execute the “cat” command, which
usually is at /usr/bin, the executable /usr/bin/cat gets executed.
– Examples: ls, cat etc.
The “type” command

• The Type command is used to find out the


information about a Linux command.
• It will show you how a given command would
be interpreted if typed on the command line.
• Easily find whether the given command is an
alias, shell built-in, file, function, or keyword .
• can find the actual path of the command
• Syntax:
– type [Options] command names

• -a : This option is used to find out whether it is


an alias, keyword or a function and it also
displays the path of an executable, if available.
• Example:
• type -a pwd
• -t : This option will display a single word as an output.
– alias – if command is a shell alias
– keyword – if command is a shell reserved word
– builtin – if command is a shell builtin
– function – if command is a shell function
– file – if command is a disk file
• Example:
• type -t pwd  builtin
• type -t cp  file
• type -t ls  alias
• type -t while  keyword
• -p : This option displays the name of the disk
file which would be executed by the shell. It
will return nothing if the command is not a
disk file.
• Example:
• type -p dash
• $ type -t ls
– Alias
• $ type -t mkdir
– file
• $ type -t pwd
– builtin
• $ type -t if
– Keyword
• type wc
– /usr/bin/wc
The man command

• The man command gives users access to manual


pages for command line utilities and tools.
• man command in Linux is used to display the user
manual of any command that we can run on the
terminal.
• man is the system's manual pager. Each page
argument given to man is normally the name of a
program, utility or function. The manual page
associated with each of these arguments is then
found and displayed.
It provides a detailed view of the command which
includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS,
EXIT STATUS, RETURN VALUES, ERRORS, FILES,
VERSIONS, EXAMPLES, AUTHORS and SEE ALSO.
• E.g man mkdir (manual of mkdir command)
• Name: mkdir
• Synopsis:
• Description about mkdir command
• Options
• Examples
• Errors
• Return values
• Files
• author
• Syntax :
$man [OPTION]... [COMMAND NAME]...

• No Option: It displays the whole manual of the


command.
• Example-1:
• To Display the manual page for the item
(program) ls :
• $ man ls
• output:
• it will display man page of ls command
• Section-num: Since a manual is divided into
multiple sections so this option is used to
display only a specific section of a manual.
• Syntax:
$ man [SECTION-NUM] [COMMAND NAME]

• Example:
$ man 2 intro
• In this example, the manual pages of
command ‘intro‘ are returned which lies in the
section 2.
-f option: One may not be able to remember the sections
in which a command is present. So this option gives the
section in which the given command is present.
Syntax:
$ man -f [COMMAND NAME]
Example:
$ man -f ls
Output: In this example, the command ‘ls‘ is
returned with its section number.
• -a option: This option helps us to display all the
available intro manual pages in succession.
• Syntax:
$ man -a [COMMAND NAME]

• Example
$ man -a intro

• Output
• In this example you can move through the manual
pages(sections) i.e either reading(by pressing Enter) or
skipping(by pressing ctrl+D) or exiting(by pressing
ctrl+C).
• -k option: This option searches the given command
as a regular expression in all the manuals and it
returns the manual pages with the section number in
which it is found.
$ man -k [COMMAND NAME]

• Example:
$ man -k cd

• Output
• The command ‘cd‘ is searched in all the manual
pages by considering it as a regular expression.
• -w option: This option returns the location in which
the manual page of a given command is present.
• $ man –w [ COMMAND NAME]

• Example
• $man -w ls

• Output
• The location of command ‘ls‘ is returned.
• -I option: It considers the command as case
sensitive.
• $ man –I [ COMMAND NAME]

• Example
• $man -I printf
• Output:
• The command ‘printf‘ is taken as case-
sensitive i.e ‘printf‘ returns the manual pages
but ‘Printf‘ gives error.
Operating system Processes

• A process is a program in execution.


• A process is more than the program code, which is
sometimes known as the text section.
• It also includes the current activity, as represented by the
value of the program counter and the contents of the
processor's registers.
• A process generally also includes the process stack, which
contains temporary data (such as function parameters,
return addresses, and local variables), and a data section,
which contains global variables.
• A process may also include a heap, which is memory that
is dynamically allocated during process run time.
Process in memory
• A program is a passive entity, such as a file containing a list of
instructions stored on disk (often called an executable file),
• whereas a process is an active entity, with a program counter
specifying the next instruction to execute and a set of
associated resources.
• A program becomes a process when an executable file is
loaded into memory. (RAM)
• Two common techniques for loading executable files are
double-clicking an icon representing the executable file and
entering the name of the executable file on the command
line (as in prog. exe or a. out.)
Process State
• As a proces; excutes, it changes state.
• The state of a process is defined in part by the current
activity of that process. Each process may be in one of
the following states:
• New: The process is being created.
• Running: Instructions are being executed.
• Waiting. The process is waiting for some event to occur
(such as an I/0 completion or reception of a signal).
• Ready. The process is waiting to be assigned to a
processor.
• Terminated. The process has finished execution.
Five-state Process Model
Mechanism of process creation

• A process may create several new processes, via a create-


process system call, during the course of execution.
• A new process can be created by the fork() system call.
The new process consists of a copy of the address space
of the original process.
• fork() creates new process from existing process
• The creating process is called a parent process, and the
new processes are called the children of that process.
• Each of these new processes may in turn create other
processes, forming a tree of processes.
• Fork() system call is situated in <sys/types.h> library.
• The function is called from parent process.
• Both the parent and the child processes continue
execution at the instruction after the fork(), the
return code for the fork() is zero for the new process,
whereas the process identifier(PID) of the child is
returned to the parent.
• Most operating systems (including UNIX and the
Windows family of operating systems) identify
processes according to a unique process identifier (or
pid), which is typically an integer number
• System call getpid() returns the Process ID of
the current process and getppid() returns the
process ID of the current process’s parent
process.

• getpid()getprocessid
• getppid() getparentprocessid
Parent Process

• All the processes in operating system are created when a


process executes the fork() system call except the startup
process.
• The process that used the fork() system call is the parent
process. In other words, a parent process is one that creates
a child process. A parent process may have multiple child
processes but a child process only one parent process.
• On the success of a fork() system call, the PID of the child
process is returned to the parent process and 0 is returned to
the child process. On the failure of a fork() system call, -1 is
returned to the parent process and a child process is not
created.
Child Process

• A child process is a process created by a parent


process in operating system using a fork() system call.
• A child process may also be called a subprocess or a
subtask.
• A child process is created as its parent process’s copy
and inherits most of its attributes. If a child process
has no parent process, it was created directly by the
kernel.
• If a child process exits or is interrupted, then a
SIGCHLD signal is send to the parent process.
The “ps” command

• ps command is used to list the currently running processes and


their PIDs along with some other information depends on
different options.
• It reads the process information from the virtual files
in /proc file-system. /proc contains virtual files, this is the
reason it’s referred as a virtual file system.
• It supports searching for processes by user, group, process id
or executable name. Related commands include pgrep that
supports searching for processes and pkill that can kill
processes based on a search.
• Syntax –
• ps [options]
• Options
Option Description
Displays all processes on a terminal, with the
-a
exception of group leaders.
-c Displays scheduler data.
Displays all processes with the exception of
-d
session leaders.
-e Displays all processes.
-f Displays a full listing.
-glist Displays data for the list of group leader IDs.
-j Displays the process group ID and session ID.
-l Displays a long listing
-plist Displays data for the list of process IDs.
-slist Displays data for the list of session leader IDs.
-tlist Displays data for the list of terminals.
-ulist Displays data for the list of usernames.
• ps –a
• ps –c
• ps –d
• ps -e
• Options for ps Command :

Simple process selection : Shows the processes for the current shell –
[root@rhel7 ~]# ps
• PID TTY TIME CMD
• 12330 pts/0 00:00:00 bash
• 21621 pts/0 00:00:00 ps
• Result contains four columns of information.
Where,
PID – the unique process ID
TTY – terminal type that the user is logged into
TIME – amount of CPU in minutes and seconds that the process has been
running
CMD – name of the command that launched the process.
• View Processes : View all the running processes use
either of the following option with ps –
• [root@rhel7 ~]# ps -A
• [root@rhel7 ~]# ps -e

• View Processes not associated with a terminal : View all


processes except both session leaders and processes not
associated with a terminal.
• [root@rhel7 ~]# ps -a
• PID TTY TIME CMD
• 27011 pts/0 00:00:00 man
• 27016 pts/0 00:00:00 less
• 27499 pts/1 00:00:00 ps
• View all the processes except session
leaders :[root@rhel7 ~]# ps -d
• View all processes associated with this
terminal :
• [root@rhel7 ~]# ps -T
• View all the running processes :
• [root@rhel7 ~]# ps -r
• view all processes except those that fulfill the
specified conditions (negates the selection) :
Example – If you want to see only session
leader and processes not associated with a
terminal. Then, run
• [root@rhel7 ~]# ps -a -N
– OR
• [root@rhel7 ~]# ps -a --deselect
Executing a command at a specified point of
time: “at” command.
• at command is a command-line utility that is used to
schedule a command to be executed at a particular
time in the future.
• Jobs created with at command are executed only
once.
• The at command can be used to execute any
program or mail at any time in the future.
• It executes commands at a particular time and
accepts times of the form HH:MM to run a job at a
specific time of day.
• It can be useful if we want to execute a process after
some time, such as a shutdown system, taking
backups, sending emails as reminders, and more.
• The following expression like noon, midnight,
teatime, tomorrow, next week, next Monday, etc.
could be used with at command to schedule a job.

• Syntax:
• at [OPTION...] runtime
• Options:
-V: It is used to display the number to standard errors and
exit successfully.
-q: It is used to specify the queue. The queue designation
consists of a letter. It ranges from a to z or A to Z. The
queue is the default queue, and the b queue is used for
the batch. The queues with the higher letters run with
increased priority. The queue '=' is a special queue; it is
reserved for the currently running jobs.
-m: It is used to send mail to the user when the job is
successfully executed. It will work if there is no output.
-M: It is used to never send mail to the user.
-f: It is used to read the job from the file rather than standard
output.
-t: It is used to specify the time to run a job; it will be in the
format [[CC]YY]MMDDhhmm[.ss].
-l: The -l option is an alias for the atq command.
-r: The -r option is an alias for the atrm command.
-d: The -d option is an alias for the atrm command.
-b: The -b option is an alias for the batch command.
-v: The -v option is used to display the time of the job before
execution.
The nice command
• nice command in Linux helps in execution of a
program/process with modified scheduling priority.
• It launches a process with a user-defined scheduling
priority. In this, if we give a process a higher priority, then
Kernel will allocate more CPU time to that process.
• Run COMMAND with an adjusted niceness, which affects
process scheduling. With no COMMAND, print the
current niceness. Nicenesses range from -20 (most
favorable scheduling) to 19 (least favorable).
• SYNOPSIS
• nice [OPTION] [COMMAND [ARG]...]

Tag Description

-n, --adjustment=N add integer N to the niceness (default 10)

--help
display this help and exit
--version output version information and exit
• 1. To check the nice value(priority) of a process.
– ps -el | grep terminal
• 2. To set the priority of a process
– nice -10 gnome-terminal
• 3. To set the negative priority for a process
– nice --10 gnome-terminal
• 4. changing priority of the running process.
– sudo renice -n 15 -p 77982
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.
• Example:
• $ls ch*.doc &
• This displays all those files the names of which start
with ch and end with .doc −
• ch01-1.doc ch010.doc ch02.doc ch03-2.doc
ch04-1.doc ch040.doc ch05.doc ch06-2.doc
ch01-2.doc ch02-1.doc
The bg and fg commands
• bg sends the specified jobs to the background.
• A background job is executed simultaneously with fish,
and does not have access to the keyboard.
• If no job is specified, the last job to be used is put in the
background.
• The PID of the desired process is usually found by using
process expansion.

• SYNOPSIS
• bg [PID...]
• OPTIONS
Tag Description

[PID] If PID is specified, the jobs with the specified group ids
are put in the background.

• EXAMPLES
• To put the job with job id 0 in the background.
• bg %0
• fg command
• The fg command is like bg command except
that instead of sending a command in the
background, it runs them in the foreground
and occupies the current terminal and waits
for the process to exit.
• # jobs
• [1]- Stopped makewhatis
• [2]+ Stopped vi
• Without any argument, fg will run the current job in
the foreground (vi in this case).
• # fg Vi
• As the command is running in the foreground, we
don't get back the terminal until command exits. So,
jobs command will now show only one job as vi will
exit by now.
• # jobs
• [1]+ Stopped makewhatis
• # fg %1
• makewhatis
• # jobs
The kill command
• kill command in Linux (located in /bin/kill), is a built-
in command which is used to terminate processes
manually.
• kill command sends a signal to a process which
terminates the process.
• If the user doesn’t specify any signal which is to be
sent along with kill command then
default TERM signal is sent that terminates the
process.
• SYNOPSIS
• kill [ -s signal | -p ] [ -a ] [ -- ] pid ...
kill -l [ signal ]

• 1. kill -l :To display all the available signals you can use below
command option:
• Syntax:
• $kill –l
• 2. kill pid : To show how to use a PID with
the kill command.
• Syntax:
• $kill pid
• 3. kill -s : To show how to send signal to processes.
• Syntax:
• kill {-signal | -s signal} pid
• 4. kill -L :This command is used to list available signals
in a table format.
• Syntax:
• kill {-l | --list[=signal] | -L | --table}
Example:
• Kill –l
• Kill –L
• Kill –list[=signal]
• Kill -- table
The find command
• The find command in UNIX is a command line
utility for walking a file hierarchy.
• It can be used to find files and directories and
perform subsequent operations on them.
• It supports searching by file, folder, name,
creation date, modification date, owner and
permissions.
• By using the ‘-exec’ other UNIX commands can be
executed on files or folders found.
• Syntax :
• $ find [where to start searching from] [expression determines what
to find] [-options] [what to find]

• Listing files in the current directory


• $ find .

• Searching files within a specified directory


• $ find directory_name(Folder)
• Example : To search for all files in /boot execute the command
• $ find /boot
• Searching files using the filename within a specified
directory
• $ find directory_name -name "file_name“
• Example : To search for Apache2 files in /etc directory
run
• $ find /etc -name "apache2“
• $ find /movies –name “war”

• Recursively find all files with a specified file extension


• $ find directory_name -name "*.extension“
• Example: to search for all configuration files (.conf)
in /etc directory, execute
• $ find /etc -name "*.conf"
• Search for empty files and directories.
• $ find ./GFG –empty

• Search text within multiple files.


• $ find ./ -type f -name "*.txt" -exec grep 'Geek' {} \;

• Search for file with entered permissions.


• $ find ./GFG -perm 664
• This command find all the files in the GFG directory or
sub-directory with the given permissions.

You might also like