ITLSA1-22 Week7 Linux Processes

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

Linux Processes

Week 7
ITLSA1-22
Linux Processes
2. Process Status
• Linux is a multitasking operating system
• It can run more than one process at a time.
• Linux users can check on the status of running programs and influence the running of those
programs.

• Any executing program is called a process with a unique Process IDentification number
(PID).
• Although Linux can schedule many processes, in reality, the CPU can handle only one
process at a time.
• Linux switches between processes very quickly, making it appear as though all the processes
are running simultaneously.
• Linux is called a time-sharing system because many processes share CPU time.

ITLSA1-22_Week 7: Linux Processes


Linux Processes
2. Process Status
• Monitor the status of a process by using the process status command:
• ps [options]

When using the ps command:


• If no options are specified, the command will display process status information for all
processes associated with your terminal.
• A: displays information about every process running on a terminal.
• u: displays information in a user-friendly format.
• x: displays processes that do not have a controlling terminal.
• PID: displays information on a specific process number.

Four fields of information are shown by ps command:


• PID
• Terminal name
• Execution time used so far
• Command name
ITLSA1-22_Week 7: Linux Processes
Linux Processes
2. Process Status

When you run ps -A, you will get an The ps a command in Linux is used to display information about
output listing all the processes with all the processes that are associated with a terminal, including
various details, typically including: those belonging to other users.

• PID: Process ID, the unique identifier • PID: Process ID, the unique identifier for each process.
for each process. • TTY: Terminal associated with the process (if any).
• TTY: Terminal associated with the • STAT: Process state (e.g., S for sleeping, R for running, etc.).
process (if any). • TIME: The amount of CPU time consumed by the process.
• TIME: The amount of CPU time • COMMAND: The command that started the process.
consumed by the process.
• CMD: The command that started the
process.

ITLSA1-22_Week 7: Linux Processes


Linux Processes
3. Running Processes in the Background
• Standard Execution
• Typically, an executing process must be completed before another can run.
• Background Execution
• Processes can run in the background.
• This allows the user to continue other work at the prompt.
• System Performance
• Running too many background processes can slow down the system.
• Having too many background processes may cancel out any time savings.

• To prevent the output of background processes from getting mixed up with the output of
foreground processes, redirect the output of the background processes to a file.
• To prevent other conflicts, do not run background processes that require input from the
keyboard.
ITLSA1-22_Week 7: Linux Processes
Linux Processes
3. Running Processes in the Background
Background Processes
• Run independently of user control and do not occupy the terminal.
• They run behind the scenes, allowing the user to continue using the terminal for other commands.
• They are used for tasks that do not require user interaction or that take a long time to complete.

Examples:
• Running a Command in the Background:
• sleep 100 &
• The sleep command runs in the background for 100 seconds, freeing the terminal for other use.
• Starting a Graphical Application:
• gedit &
• The gedit text editor runs in the background, freeing the terminal

ITLSA1-22_Week 7: Linux Processes


Linux Processes
3. Running Processes in the Background
Foreground Processes
• Foreground processes are processes that run directly under the control of the user.
• They are initiated and interact with the user via the terminal or graphical interface.
• The terminal remains occupied by the process until it is completed.
• User inputs are directly sent to the process.

Examples:
• Running a Text Editor:
• vi week7.txt
• The terminal is occupied by vi and user input is directed to editing week7.txt.
• Executing a Command:
• ls -la
• The ls command runs in the foreground and the terminal displays the output of the directory listing.
ITLSA1-22_Week 7: Linux Processes
Linux Processes
3. Running Processes in the Background
To run a process in the background:
• When issuing a command to run a process in the background, a PID is displayed, and the user
is returned to the prompt to continue with other work.

1. Run the find command in the background: find / -type d > found 2> errorlog &
2. Note the PID: e.g [1] 1234 - the PID of the background process
3. Check running processes using ps aux command:
4. Compare with the PID of the background process
5. Continue with other work in the foreground
6. Check the contents of the found file: more found
7. Cancel the more command if the listing gets too long: <Ctrl><c>

Note: [1] – is a job number, also called job ID


ITLSA1-22_Week 7: Linux Processes
Linux Processes
4. Terminal Processes
There are times when it is necessary to terminate a process prematurely.
Linux uses many signals to achieve this, such as the hangup, interrupt, quit, terminate, and kill signals.

1. Hangup (SIGHUP) Signal


• When a user logs off, the system sends a hangup signal (SIGHUP) to all processes related to that user, terminating them.
• This signal can be redirected or ignored using the nohup command to prevent unintended termination.
2. Interrupt (SIGINT) Signal
• Pressing <Ctrl><c> sends an interrupt signal (SIGINT) to terminate a foreground process.
• This signal is used to stop processes that are running in the foreground.
3. Quit (SIGQUIT) Signal
• Pressing <Ctrl><\> sends a quit signal (SIGQUIT) to a process, causing it to terminate and produce a core dump if applicable.
4. Terminate (SIGTERM) Signal
• Is used to terminate a process by its PID.
• Processes can catch this signal to perform cleanup operations before exiting.
5. Kill (SIGKILL) Signal
• Is used to terminate a process forcefully (execute a sure kill)
• Unlike SIGTERM, SIGKILL cannot be caught or ignored by the process, making it a last resort for terminating stubborn processes.
ITLSA1-22_Week 7: Linux Processes
Linux Processes
4. Terminal Processes
Examples: Signal Usage – run the commands to test each signal

1. Hangup (SIGHUP) Signal - running a process and logging out, causing SIGHUP to be sent
• sleep 300 &: start a process in the background by running the command
• exit: close the terminal, this will send a hangup signal to the background processes
2. Interrupt (SIGINT) Signal - interrupting a long-running process with Ctrl+C
• ping google.com: start a process that runs indefinitely
• Press ctrl+c to send SIGINT
3. Terminate (SIGTERM) Signal - terminating a process using kill with SIGTERM
• tail -f /var/log/syslog: start a long-running process
• ps -ef | grep tail: open another terminal, find the PID, and send SIGTERM
• kill PID: send SIGTERM to terminate the tail process
4. Kill (SIGKILL) Signal – terminating by force an unresponsive process using kill with SIGKILL
• sleep 1000 &: start a process, PID #### is assigned to the sleep process
• kill -9 PID
ITLSA1-22_Week 7: Linux Processes
Linux Processes
4. Terminal processes
Protecting Processes with nohup
• To prevent a process from being terminated by SIGHUP when logging off, use the nohup command followed by
the command and & to run it in the background.
• Example: nohup command &
• Output is redirected to nohup.out.
• If a command is preceded by nohup, the process will execute even if the user exits the terminal.

Handling Processes
• Check process status using the process status commands:
• ps -ef or ps aux
• The process status commands can be combined with grep to filter out the process you want to check
• Terminate a process by its PID using the kill command followed by the PID.
• Example: kill PID

ITLSA1-22_Week 7: Linux Processes


Linux Processes
4. Terminal processes
Special Cases
• Some processes may not respond to SIGTERM and require SIGKILL for termination.
• This signal always terminates any process that you own and is known as a sure kill.
• Use this signal as a last resort since processes cannot trap it and clean up first.

• To view all available signal numbers


• Run the command: kill -l

ITLSA1-22_Week 7: Linux Processes


Linux Processes
4. Terminal processes
• Background processes are protected from interrupt and quit signals, so they cannot be used to terminate misbehaving
background processes. In cases like this, you can use the termination signal together with the PID

Example: terminate a background process


1. Run the find command in the background and redirect its output to a file:
• find / -type f > found 2>&1 &
2. Note the PID (Process ID) allocated to the find process: [job ID] PID
3. Check the background process using ps command to verify that the find process is running:
• ps -ef | grep find
4. Try sending the interrupt signal
• Attempt to send the interrupt signal using Ctrl+C
• Since the process is in the background, you might need to bring it to the foreground first using the fg command – fg %1
5. Check if the process has stopped running using ps command: ps -ef | grep find
6. Send the termination signal using the kill command: kill PID
7. Check if the process has stopped running using ps command
8. The final output – the find process should no longer appear in the list if it has been successfully terminated
ITLSA1-22_Week 7: Linux Processes
Linux Processes
4. Terminal processes
Example: Using nohup to Protect a Process

1. Start a long-running process with nohup. In this example, we'll use the find command to search for all files starting from the root
directory and run it in the background.
• nohup find ~/ -type f &

• 6102 – the process id (PID) of the background find command


2. Exit the terminal - where the nohup command was started
• exit
• Exiting the terminal normally sends a SIGHUP signal to terminate running processes.
• The nohup command has been used, and the find process continues running.
3. Open a new terminal and verify the process
• ps -ef | grep find
4. View the results of the protected process
• more nohup.out
ITLSA1-22_Week 7: Linux Processes

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy