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

pr 6

Linux process management involves controlling and organizing running programs on a computer by managing processes. It provides commands to view, prioritize, start, and stop processes, allowing users to effectively manage system resources. Key commands include 'ps' for listing processes, 'kill' for terminating processes, and 'nice' for setting process priority.

Uploaded by

shindegauri384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

pr 6

Linux process management involves controlling and organizing running programs on a computer by managing processes. It provides commands to view, prioritize, start, and stop processes, allowing users to effectively manage system resources. Key commands include 'ps' for listing processes, 'kill' for terminating processes, and 'nice' for setting process priority.

Uploaded by

shindegauri384
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

What is Linux Process Management?

Linux process management is the way to control and organize all the
programs running on your computer. When you run a program on Linux it
creates something called a "process". Process management allows you to
see the list of all the processes currently running on your system. It also lets
you start the new processes which means running the new programs. If there
is a process you no longer need you can stop it and remove that program
from the system.
Additionally The process management gives you the ability to decide which
processes are most important. You can make some processes a higher
priority so they run better than other processes. Overall process
management helps you keep track of what is running on your Linux computer
and have control over those running programs.

Linux Process Management Commands and


Description
Commands Description Example

This command will show you the information ps aux


ps about the programs that are currently running Shows all running
on your computer. processes

This command will lets you see how much of


top
the computers resources (like CPU and
Displays real-time
top memory) are being used and as well as the
process
details about the running programs and
information
processes.

kill 1234
This command wil allow you to stop the
kill Kills process with
running program by sending it a signal.
PID 1234

killall
This command will stops all the running
firefox
killall programs that match a specific name that you
Kills all Firefox
will provide.
processes

pkill This command will let you search for the pkill -9
Commands Description Example

httpd
program and to stop a program based on its
Forcefully kills
name or the other details.
the httpd process

pgrep chrome
This command will help you find the program
Shows PIDs of
pgrep by name or with other attributes and gives you
Chrome
its process ID number.
processes

nice -n 10
This command will sets the priority level of the
script.sh
nice program that making it more or less important
Runs script.sh at
compared to other programs.
lower priority

renice -n 5
This command will change the priority level of 1234
renice
the program that are already running. Changes priority
of PID 1234 to 5

jobs
This command will show you the information
Lists jobs in the
jobs about the programs that are running into your
current shell
current shell session.
session

This command will bring a program that are fg %1


fg running in the background or into the Brings job 1 to
foreground. the foreground

bg %2
This command will send the program to run
bg Sends job 2 to
into the background.
the background

nohup
This command will run a program in the script.sh &
nohup background and it will keep it running even Runs script.sh in
after you close the shell session. background
persistently
Commands Description Example

This command will create the virtual terminal screen


screen session that you can detach and then reattach Starts a new
it to at any time. screen session

This command will display a detailed list of all ps aux


ps aux the running programs on the system including Shows detailed
those started by the users. process list

systemctl
This command will manage the system
status nginx
systemctl services and the processes into the your
Shows status of
system.
Nginx service

This command will show the real time system top -H


resource usage like CPU, memory, and the Displays
top -H
details about running programs in a processes in a
hierarchical view. tree view

pstree
This command will displays the system
pstree Shows process
processes into a tree like structures.
tree

lsof -i
This command will lists open the files and the Lists open
lsof
programs that are using onto them. network file
descriptors

The ps command in Linux is used to display information about running processes on the system. It
stands for "process status" and shows details like process ID (PID), memory usage, CPU usage, and more.
Here's a breakdown of how to use the ps command with examples.

Syntax:

ps [options]
Common Options:

1. ps (without any options):


o Displays processes running in the current terminal session.

Example:

$ ps

o/p:

PID TTY TIME CMD

2345 pts/0 00:00:02 bash

2456 pts/0 00:00:00 ps

 PID: Process ID.


 TTY: Terminal associated with the process.
 TIME: Amount of CPU time the process has used.
 CMD: The command that launched the process.

ps aux:

Lists all processes running on the system, including those not associated with the terminal

Eg ps aux

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

root 1 0.0 0.1 169056 5524 ? Ss Mar16 0:02 /sbin/init

user 1234 0.1 1.2 249328 15756 ? S Mar16 0:10 /usr/bin/firefox

user 5678 0.0 0.5 345678 10456 pts/0 Ss 09:00 0:00 bash

user 2345 0.0 0.0 13360 1992 pts/0 R+ 10:00 0:00 ps aux

Explanation:

 USER: The user who owns the process.


 PID: The process ID.
 %CPU: CPU usage.
 %MEM: Memory usage.
 VSZ: Virtual memory size (in KB).
 RSS: Resident Set Size (physical memory used).
 STAT: Process state (e.g., S for sleeping, R for running).
 START: When the process started.
 TIME: CPU time used by the process.
 COMMAND: The command that launched the process.

ps -ef:

 This is another way to show all processes on the system, with a slightly different format.
It's similar to ps aux, but -ef provides a standardized output format for all systems.

ps -u username:

 Lists all processes owned by a specific user.

ps -p PID:

 Displays information about a specific process using its PID.

fg command

The fg command in Linux is used to bring a background process to the foreground.

When a process is running in the background, the fg command allows you to interact with it as though
it is running in the foreground, allowing you to see its output or interact with it directly.

Syntax:

fg [job_id]

 job_id is the ID of the job you want to bring to the foreground. This is specified by the
job number assigned when the process was sent to the background.

If you don't specify a job_id, the fg command will bring the most recently backgrounded job to
the foreground.
Example Usage:

1. Running a Command in the Background: First, let’s start a command in the


background using &:

$ sleep 100 &

 This starts the sleep 100 command in the background.

 List the Background Jobs: Use the jobs command to list all background jobs and their
associated job IDs:

$ jobs

o/p:

[1]+ 2345 Running sleep 100 &

Bring the Job to the Foreground: Now, use the fg command to bring job [1] (the one with ID
2345) to the foreground:

bash
Copy

$ fg %1

 This will bring the backgrounded sleep 100 command to the foreground, and you can now
see its output in the terminal.

The terminal will be occupied by the sleep command until it completes or you stop it with
Ctrl+C.

 Using fg Without Job ID: If you have only one background job, you can use fg without
specifying the job ID:

$ fg

 This will bring the most recent background job to the foreground.

 Multiple Jobs: If you have multiple background jobs, you can specify which one you want to
bring to the foreground:

$ fg %2
This will bring job [2] to the foreground, if job [2] is the one you want to interact with.

sleep command

The sleep command in Linux is used to pause or delay the execution of the next command for a
specified amount of time. It is often used in shell scripts or commands to introduce a delay between
operations, to allow time for processes to complete, or to create time gaps for other purposes.

Basic Syntax:

sleep [OPTION] DURATION

 DURATION specifies how long to pause. You can specify the duration in seconds, minutes,
hours, or days.

Units of Time:

 s for seconds (default if no unit is specified)


 m for minutes
 h for hours
 d for days

Examples:

1. Sleep for 5 seconds:

$ sleep 5

Sleep for 3.5 seconds (using fractional values):

The exit command in Linux is used to terminate the current shell session or script. When you
type exit, it ends your terminal session, logs you out of the shell, or finishes a script's execution.

Basic Syntax:
exit [n]

n is an optional exit status code. It is a number (0 to 255) that represents the exit status or return code
of the command or process. If no status code is provided, exit uses the default exit status of 0, which
indicates successful completion.
Example Usage:

1. Exit the current terminal session: Simply typing exit will close the terminal or shell
session.

$ exit

This will exit the current shell session. If you're using a terminal window, it will close the window.

bg command

The bg command in Linux is used to resume a stopped job and run it in the background. This is useful
when you want to continue executing a process that was stopped (e.g., using Ctrl+Z) but without it
occupying your terminal.

bg [job_id]

job_id: The ID of the job you want to resume. You can specify the job number (e.g., %1) of the job to
bring it back to the background.

If you don't specify a job_id, bg will resume the most recently stopped job.

Example Usage:

1. Start a Command in the Background: You can use the & operator to run a command in
the background from the beginning:

$ sleep 100 &

This will start the sleep 100 command in the background. You can continue using the terminal for
other tasks while the sleep command runs in the background.

kill command

The kill command in Linux is used to terminate processes. Despite its name, it doesn't necessarily
"kill" a process immediately; instead, it sends signals to processes, instructing them to perform certain
actions (such as termination, stopping, or restarting).

Syntax:

kill [options] PID

PID is the Process ID of the process you want to send the signal to.
If you don't specify an option, the default signal sent is SIGTERM (signal 15), which gracefully
asks the process to terminate.

Common Options for kill:

 -l: List all available signals.


 -9: Send the SIGKILL signal (forceful termination).
 -15: Send the SIGTERM signal (graceful termination, which is the default).
 -s SIGNAL: Send a specific signal by name (e.g., kill -s SIGKILL <PID>).
 -9: Forcefully kill the process without giving it a chance to clean up.

Example Usage:

1. Terminate a process using its PID:


o First, find the process ID (PID) of the process you want to terminate. You can use
commands like ps, top, or pgrep for this.

$ ps aux | grep firefox

o/p: user 1234 0.1 1.2 249328 15756 ? S Mar16 0:10 /usr/bin/firefox

Here, 1234 is the PID of the Firefox process. To terminate it, use the kill command:

Common Signals:

 SIGTERM (15): Default signal. Gracefully asks a process to terminate.


 SIGKILL (9): Forces a process to terminate immediately, without allowing cleanup.
 SIGSTOP: Stops (pauses) a process.
 SIGCONT: Continues a stopped process.
 SIGHUP (1): Typically used to restart a process (like restarting a daemon).
 SIGINT (2): Sent by pressing Ctrl+C, used to interrupt and terminate a running process.

killall command

The killall command in Linux is used to terminate all processes with a specified name.
Unlike the kill command, which requires a process ID (PID), killall allows you to terminate
all instances of a process by its name. This is particularly useful when you want to terminate
multiple processes of the same name without needing to find their individual PIDs.

Basic Syntax:
killall [options] <process_name>

process_name is the name of the process or command you want to terminate (e.g., firefox, python,
etc.).
Common Options:

 -9: Send the SIGKILL signal (forceful termination).


 -15: Send the SIGTERM signal (graceful termination) — this is the default.
 -e: Match the exact name of the process.
 -v: Show a verbose output, displaying the names of the processes being killed.
 -u: Kill processes owned by a specific user.

nice command

The nice command in Linux is used to set the priority of a process when it starts. It allows you to
control how much CPU time a process gets compared to other processes. The priority of a process is
controlled by a value called niceness.

Niceness Range:

 Niceness ranges from -20 to 19:


o Lower niceness values (e.g., -20) mean higher priority (the process gets more CPU
time).
o Higher niceness values (e.g., 19) mean lower priority (the process gets less CPU time).

By default, processes start with a niceness of 0.

Basic Syntax:

nice [OPTION] [COMMAND [ARGUMENTS...]]

 COMMAND: The command or program you want to run.


 OPTION: You can specify the niceness value here.

Setting Niceness When Starting a Process:

You can specify the niceness of a process when launching it with the nice command.

Example Usage:

1. Run a command with default niceness: Running a command without specifying any
niceness value will use the default niceness (0):

$ nice sleep 60
This will run the sleep command for 60 seconds with the default niceness of 0.

Important Notes:

 Default niceness is 0, and you can go as low as -20 (highest priority) or as high as 19 (lowest
priority).
 Superuser permissions are required to set negative niceness values (-1 to -20).
 Increasing the niceness (positive values) will lower the priority of the process, which might be
helpful when you want a background task to use fewer system resources.

at command

The at command in Linux is used to schedule a command or script to run once at a specific time in the
future. Unlike cron jobs, which run repeatedly at fixed intervals, at is designed for one-time tasks that
need to be executed at a specified time.

Basic Syntax:

at [OPTION] TIME

TIME: The time when you want the command to run. This can be specified in a variety of formats (e.g.,
exact time, relative time, etc.).

Important Notes:

 The at command requires the atd daemon (the at daemon) to be running on your system. This
daemon processes the jobs scheduled by at.
 You must have permission to use the at command. Some systems restrict access to the at
command, and it may need to be enabled by the system administrator.
 The at command schedules tasks to run only once, not repeatedly like cron jobs.

How to Use the at Command:

1. Schedule a Command to Run at a Specific Time: To schedule a command to run at a


specific time, you first run at followed by the time:

$ at 10:30

This opens an interactive prompt where you can type the command to execute at 10:30 AM. For

Summary:

 The at command is used to schedule a one-time task to be executed at a specific time.


 You can specify the time in various formats, including exact time, relative time (e.g., now + 10
minutes), or specific date and time.
 atq is used to view scheduled jobs, and atrm is used to remove a job.
 The at daemon must be running for the command to work.

jobs command

The jobs command in Linux is used to display a list of all the current jobs that are running or stopped in
the current shell session. These jobs are typically background processes, stopped processes, or
processes that were started but are currently waiting for user input.

Basic Syntax:

jobs [options]

Common Options:

 -l: Display the process ID (PID) of each job.


 -n: Show only jobs that have changed state since the last time the command was run.
 -p: Show only the process IDs (PIDs) of the jobs.
 -r: Show only running jobs.
 -s: Show only stopped jobs.

Example Usage:

1. List All Jobs: The jobs command without options will display all the current jobs in the
shell session.

$ jobs

Example output:

[1]+ 1234 Running sleep 100 &

[2]- 5678 Stopped ping google.com

 Job Number: [1] and [2]


 Process ID (PID): 1234 and 5678
 State: Running and Stopped
 Command: sleep 100 and ping google.com

You might also like