0% found this document useful (0 votes)
10 views

Linux Lecture 20

The document discusses various Linux processes and commands related to processes. It covers topics like: - Processes in Linux and how they are identified by a Process ID (PID). - Different types of processes like foreground and background processes. - Commands to view and manage processes like ps, pstree, kill, killall, nice, renice, nohup and bg/fg. - Concepts like process priority, session leader, signals etc. Detailed syntax and examples are provided for each command.

Uploaded by

Raghav Malik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Linux Lecture 20

The document discusses various Linux processes and commands related to processes. It covers topics like: - Processes in Linux and how they are identified by a Process ID (PID). - Different types of processes like foreground and background processes. - Commands to view and manage processes like ps, pstree, kill, killall, nice, renice, nohup and bg/fg. - Concepts like process priority, session leader, signals etc. Detailed syntax and examples are provided for each command.

Uploaded by

Raghav Malik
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

LINUX

ADMINISTRATION

Lecture – 20

Background
processes

By : Naveen Tiwari

1
Processes
• Everything that you run in Linux is a process.
• Whenever a command runs in Linux, it creates/starts a new
process.
• Eg: ls a process starts.
• A 5 digit ID number Linux keeps an account of the processes, this
number is called process ID or PID.
• Eg: PID -20968
• Each process in the system has a unique PID.
• Used up pid’s can be used in again for a newer process since all
the possible combinations are used.
• No two processes with the same pid exist in the system at a time
because it is the pid that Unix uses to track each process.
Initializing a process
• There are 2 methods to run a process
• 1. Foreground Process
• 2. Background Process
Foreground Process
• Every process when started runs in foreground by default,
receives input from the keyboard, and sends output to the
screen.
• Eg: ls, pwd,
• When a command/process is running in the foreground, no other
processes can be run or started because the prompt would not be
available until the program finishes processing and comes out.
Background Process
• A background process is a process that is started from a
shell (or terminal) and then runs independently.
• When a background process has been launched from a
terminal session, the same terminal will be immediately
available to execute other commands.
Ps (Process Status)
• It is used to view currently running processes on the system.
• It helps us to determine which process is doing what in our
system, how much memory it is using, how much CPU space
it occupies, user ID, command name, etc .
• The ps command may display different results for different
system.
Synatx : $ps [options]
Eg :
PID - The process ID of running command
TTY - The type of terminal where current command is running
TIME - Tells how much time is used by CPU to run the process
CMD - It is current command

• To view all currently running processes.


cmd : $ps

• To view all the running processes


cmd $ps –A or $ps –e
• To view all currently running processes.
cmd : $ps
• To view all processes owned by you
cmd $ps –x
• To view the process by the command name
cmd : $ps –C cmd_name
Eg :$ps –C ps
• To view the process created by group
cmd $ps –G groupname or $ps --Group groupname
Eg : $ps –G root
• To view all the running processes by group id
cmd $ps –g groupid or $ps –group groupid
Eg: ps –g 1
• To view aprocess/multi processes by process Id.
cmd $ps p processid1 processid2 or
$ps -p processid1 processid2
$ps –pid processid1 processid2
Eg : ps p 1234
Eg : ps –p 1234 432
• To view process according to user-defined format.
cmd : $ps –o col_name or ps o col_name
Eg : ps –o tty,user,pid

session leader
• All processes within a session share the same SID, which is
the same as the PID of the process that is called setsid(),
also known as the session leader.
• A session leader is any process with a PID that matches its
SID, i,e - session id == process id.

Pstree
• It shows the running processes as a tree.
• The root of the tree is either init or the process with the given
pid.
Synatx : $pstree [options] [option]
Eg:
• To view process tree
cmd $pstree
kill
• It 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.
• The most widely applied signals are as follows:
HUP (1)- It reloads a process.
KILL (9)- It kills a process.
TERM (15)- It stops a process gracefully.

Syntax: $kill [options]

• To view all the available signals.


Synatx : $kill -l

• Negative PID values are used to indicate the process group


ID.
• A PID of –1, It indicates all the processes except kill and init,
which is the parent process of all processes on the system.
• To kill a process with PID
Synatx : $kill pid
• To kill a process with PID
Synatx : $kill –9 pid

Killall
• The killall is used for killing any running process on the
system based on a given name.
• This command will terminate the processes forcibly when a
specified name matches.
• It can kill a bunch of processes altogether.
• It will first send a signal to every running daemon. If you do
not specify any signal name, by default, it sends the
SIGTERM.
• To define signal name- using the number (example -1) or
via options (example: -s).

Syntax: killall [options]


• To kill a process by name
• Cmd : $killlall processname
Kill v/s Killall

Eg : Step 1 - Start background processes


Cmd : $evince &
Cmd : $evince &
Cmd : $evince &
Cmd : $evince &
Cmd : $evince &
Cmd : $yes > /dev/null &
Cmd : $yes > /dev/null &
Cmd : $yes > /dev/null &
Step 2 – To view running process
Cmd : $jobs or $ps
Step 3 – Stop/end a process using kill
Cmd : $kill PID
Step 4 – Again check running process
Cmd : $jobs or $ps
Step 5 – Stop/end process using killall
Cmd : $killall evince
Step 6 – Again check running process
Step 7 – do the same to kill/killall other process
Nice
• nice command helps to run a program with modified scheduling
priority.
• You can set the priority of the process before it is started using nice
command.
• You cannot change the priority of the running process with nice
command.
• If we give a process a higher priority, then Kernel will allocate more
CPU time to that process.
• Niceness values range from -20 (most favorable to the process)
to 19 (least favorable to the process).
• The default nice value for processes created by a user is 0.

Syntax : $nice [OPTION] [COMMAND [ARG]...]

Eg :

• To view nice value of the running process.


Cmd : $ ps –l or $ps –el or $ top
The NI column represents the nice value, whereas the PRI column
represents the priority value.
• To view default value of nice.
Cmd : $ nice

• To set the priority of process


Cmd : $nice -n nice_value command
Or $nice –nicevalue command
Or $ nice --adjustment=nice_value command
Eg : $nice –n 10 top

• To increase the priority of a process.


Cmd : $ nice -<-nice_value> command
Or $ nice -n <nice_value> command
Eg : $nice --10 top
Note : Only root or sudo user.set can set the negative nice of a
command.
Eg : $sudo nice --10 top or $sudo nice –n -10 top

• To decrease the priority of a process.


Cmd : $ nice -<+nice_value> command
Or $ nice -n <nice_value> command
Eg : $sudo nice -+10 top or $sudo nice –n +10 top
renice
• renice command allows you to change and modify the scheduling
priority of an already running process.
• The super-user can alter the priority of any process and set the
priority to any value. -p or --pid option is used to specify process ID.

Syntax : $renice [-n] priority [-g|-p|-u] identifierq


Eg :

• To change priority of existing process


Cmd : $ sudo renice -n nice_value -p process_id
Or $ sudo renice -n nice_value --pid process_id
Eg : sudo renice –n 10 –p 1111

• To change the priority of all processes of a specific group


Cmd : $ sudo renice -n nice_value -g group_id
Or $ sudo renice -n nice_value --pgrp group_id
Eg : sudo renice –n 10 –g 1000

• To change the priority of all programs of a specific user.


Cmd : $ sudo renice nice_value -u <name/id>
Or $ sudo renice nice_value --user <name/id>
Nohup (no hang up )
• It keeps the processes running even after exiting the terminal
orshell.
• It prevents the processes from getting the SIGHUP signals (Signal
hang up); these signals are sent to the process to terminate or end a
process.
• nohup (No Hang Up) is a command in Linux systems that runs the
process even after logging out from the shell/terminal.

Syntax : $nohup command [command-argument ...]


Eg :

• Runs the command in the foreground and redirects the command


output to the nohup.out file if any redirecting filename is not
mentioned. it will create a file 'nohup.out' and append the output to
it.
Cmd : $nohup bash hello.sh or $nohup ./hello.sh
• To display the output, execute the below command
Cmd : cat nohup.out

• To run a process in a background


$nohup bash hello.sh &
$nohup ping www.google.com &
• To bring the command in the foreground.
Cmd : $fg or $pgrep -a ping

• To run multiple commands in the background


Cmd : $ nohup bash -c 'commands'

Eg : $nohup bash -c 'cal && date'

To check out of nohup - $cat nohup.out

• Redirecting Output to a Different File


Cmd : $nohup bash -c 'cal && date' > outputfile
Bg (background)
• This is used to place foreground jobs in background.
• It resumes execution of a suspended process as if they had been
started with &.
• Use bg command to restart a stopped background process.

Syntax : $bg [job_spec ...]


$bg jobID
$bg jobID1 jobID2 ... jobIDN

• The JobID can be a process ID (PID) number, or you can use one of
the following symbol combinations:
%Number : Use the job number such as %1 or %2.
%String : Use the string whose name begins with suspended
command such as %commandNameHere or %ping.
%+ OR %% : Refers to the current job.
%- : Refers to the previous job

Eg :
Eg :bg process

Eg : Step 1 - Start a process


Cmd : $ping www.google.com Press
Step 2 – close the process – ctrl+z
Step 3 – to resume a stopped process in background
Cmd : $bg %1
Step 4 – to check running process
Cmd : $jobs –l
Step 5 – to kill runng process
Cmd $ kill pid
Step 5 – again check running process
Cmd : $jobs -l
Fg (foreground)
• fg command in linux used to put a background job in foreground.

Syntax : $bg [job_spec ...]


$fg jobID
$fg jobID1 jobID2 ... jobIDN

• The JobID can be a process ID (PID) number, or you can use one of
the following symbol combinations:
• %Number : Use the job number such as %1 or %2.
• %String : Use the string whose name begins with suspended
command such as %commandNameHere or %ping.
• %+ OR %% : Refers to the current job.
• %- : Refers to the previous job.

Eg : Step 1 - Start a process


Cmd : $ping www.google.com Press
Step 2 – close the process – ctrl+z
Step 3 – to resume a stopped process in foreground
Cmd : $fg %1
Cmd $ kill pid

You might also like