ITLSA1-22 Week7 Linux Processes
ITLSA1-22 Week7 Linux Processes
ITLSA1-22 Week7 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.
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.
• 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
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>
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
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 &