pr 6
pr 6
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.
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
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
systemctl
This command will manage the system
status nginx
systemctl services and the processes into the your
Shows status of
system.
Nginx service
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:
Example:
$ ps
o/p:
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
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:
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:
ps -p PID:
fg command
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:
List the Background Jobs: Use the jobs command to list all background jobs and their
associated job IDs:
$ jobs
o/p:
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:
DURATION specifies how long to pause. You can specify the duration in seconds, minutes,
hours, or days.
Units of Time:
Examples:
$ sleep 5
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:
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:
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.
Example Usage:
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:
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:
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:
Basic Syntax:
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.
$ at 10:30
This opens an interactive prompt where you can type the command to execute at 10:30 AM. For
Summary:
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:
Example Usage:
1. List All Jobs: The jobs command without options will display all the current jobs in the
shell session.
$ jobs
Example output: