Introduction
In this lab, you will learn the essential skills for managing and monitoring processes on a Linux system. You will explore how to interact with processes running in both the foreground and background, giving you greater control over your command-line environment and system resources. This hands-on experience is fundamental for anyone working with Linux, from system administrators to developers.
You will begin by starting a background job using the & operator and viewing its status with jobs. You will then inspect running processes with ps, monitor real-time system activity with top, and practice job control using fg, bg, and Ctrl-Z. To complete the lab, you will learn how to adjust a process's priority with renice and terminate it using the kill command, covering the full lifecycle of process management.
Start and View a Background Process with & and jobs
In this step, you will learn how to run a command in the background and how to view the status of background jobs. In a Linux shell, you typically run a command and wait for it to complete before the prompt returns. This is called running a process in the foreground. However, for long-running tasks, you might want to run them in the background so you can continue using your terminal for other commands.
To run a command in the background, you simply add an ampersand (&) at the end of the command line. Let's try this with the sleep command, which is a simple utility that pauses for a specified amount of time.
Execute the following command to run sleep for 300 seconds in the background. This will allow us to work with it in the next steps.
sleep 300 &
After you press Enter, you will see something similar to the following output, and the command prompt will return immediately, allowing you to type new commands.
[1] 12345
The shell has started the sleep 300 command as a background job. The output provides two key pieces of information:
[1]: This is the job ID. The shell assigns a unique job ID to each background process.12345: This is the Process ID (PID). The operating system assigns a unique PID to every running process. Your PID will be different from the example.
Now that the process is running in the background, how can you check its status? You can use the jobs command, which lists all the jobs running in the background of the current shell session.
Run the jobs command in your terminal:
jobs
The output will show you the sleep command we just started, along with its job ID and current status.
[1]+ Running sleep 300 &
You have successfully started a process in the background and know how to check its status. This is a fundamental skill for managing long-running tasks on a Linux system. In the following steps, we will explore how to interact with this background job.
Inspect Running Processes with ps
In the previous step, you used the jobs command to see the background process in your current shell. However, jobs is limited to your session. To get a broader view of all processes running on the system, you need a more powerful tool: ps (process status). The ps command provides a snapshot of the current processes at the moment you run it.
Let's start by running ps without any options. This command provides a snapshot of the processes owned by the current user and attached to the current terminal.
ps
The output will be minimal, likely showing just your shell (zsh) and the ps command you just ran. The PIDs will differ on your system.
PID TTY TIME CMD
23882 pts/0 00:00:00 zsh
23953 pts/0 00:00:00 ps
To see all processes running on the system, not just the ones in your terminal, you can use ps with options. A very common and useful combination is ps aux.
a: shows processes for all users.u: displays in a user-oriented format (showing user, CPU%, MEM%, etc.).x: includes processes not attached to a terminal.
A long list of processes isn't very useful if you're looking for something specific. We can combine ps with the grep command to filter the output. Let's find the sleep process you started in the previous step.
ps aux | grep sleep
This command filters the ps aux output and shows only the lines containing the word "sleep".
labex 23885 0.0 0.0 7264 868 pts/0 S 11:50 0:00 sleep 300
labex 23962 0.0 0.0 10788 2240 pts/0 S+ 11:52 0:00 grep --color=auto sleep
You'll likely see two lines in the output. One is your sleep 300 process. The other is the grep sleep command itself, which was running at the moment ps captured the process list. Notice the PID for sleep 300 (in this example, 23885) matches the one you saw when you first ran the command in the background.
Another popular format for viewing processes is ps -ef.
-e: selects every process on the system.-f: displays a "full" format listing, which includes useful information like the Parent Process ID (PPID).
Let's try it, again using grep to find our sleep process.
ps -ef | grep sleep
The output format is different, but it provides similar information. This view is particularly useful for seeing the process hierarchy via the PID and PPID columns.
UID PID PPID C STIME TTY TIME CMD
labex 23885 23882 0 11:50 pts/0 00:00:00 sleep 300
labex 23964 23882 0 11:53 pts/0 00:00:00 grep --color=auto sleep
You've now seen how to use ps to get a snapshot of system processes. By combining it with tools like grep, you can quickly find and inspect specific processes.
Monitor System Resources with top
In this step, you'll learn to use top, a powerful tool for real-time system monitoring. While ps gives you a static snapshot of processes, top provides a dynamic, continuously updated view of system activity, making it invaluable for identifying resource-intensive processes as they happen.
To start, simply type top in your terminal and press Enter.
top
Your entire terminal window will be taken over by the top interface. It will look something like this, with the data refreshing every few seconds.
top - 12:05:15 up 15 min, 1 user, load average: 0.00, 0.01, 0.00
Tasks: 115 total, 1 running, 114 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.1 us, 0.1 sy, 0.0 ni, 99.8 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1987.2 total, 985.4 free, 501.8 used, 500.0 buff/cache
MiB Swap: 2048.0 total, 2048.0 free, 0.0 used. 1325.4 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
1 root 20 0 167900 12936 8488 S 0.0 0.6 0:01.15 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd
...
The top interface is divided into two main parts:
- The summary area at the top shows system-wide statistics like uptime, number of tasks, CPU load (
%Cpu(s)), and memory usage (MiB Mem). - The process list below shows individual processes, sorted by CPU usage (
%CPU) by default.
The top command is interactive. You can use various keystrokes to change its behavior. Let's try a few:
- Sort by Memory Usage: Press
M(uppercase 'm'). The list will re-sort based on the%MEMcolumn, showing the most memory-intensive processes at the top. - Sort by CPU Usage: Press
P(uppercase 'p'). This will sort the list back to the default, by the%CPUcolumn. - Find your
sleepprocess: You can try to spot thesleepprocess you started earlier. It will likely be far down the list as it's not using any CPU. You can use theDown ArrowandUp Arrowkeys to scroll through the process list.
When you are finished observing, you can exit top at any time.
- Exit
top: Pressqto quit thetopinterface and return to your command prompt.
You've now used top to get a real-time view of your system's processes and resource usage. This is a go-to command for any system administrator troubleshooting performance issues.
Manage Job Control with fg, bg, and Ctrl-Z
In this step, you'll learn how to manage the state of your running jobs. You already know how to start a job in the background, but what if you start a long-running command in the foreground and then realize you need your terminal back? Job control allows you to move processes between the foreground and background, and to pause (stop) and resume them.
Let's work with the sleep 300 process you started earlier. First, check its status with the jobs command to ensure it's still running.
jobs
You should see your sleep job running in the background.
[1] + running sleep 300
Now, let's bring this job to the foreground. To do this, use the fg (foreground) command followed by the job ID prefixed with a %. Since our job ID is 1, the command is:
fg %1
The shell will display the command name with additional job information, and your command prompt will disappear. The terminal is now "occupied" by the sleep 300 command, waiting for it to finish.
[1] + 394 running sleep 300
To get your terminal back without killing the process, you can stop it. Press the key combination Ctrl-Z (hold down the Ctrl key and press Z).
This action sends a special signal (SIGTSTP) to the process, which pauses its execution. The process is not terminated; it's simply suspended. You'll see a message confirming this, and your prompt will return.
[1] + 394 suspended sleep 300
Now, check the status of your jobs again:
jobs
The output now shows the job's status as "suspended".
[1] + suspended sleep 300
A stopped job can be resumed. You can resume it in the foreground with fg or in the background with bg. Let's resume it in the background using the bg command.
bg %1
The shell will confirm that the job is now running in the background again.
[1] + 394 continued sleep 300
You can verify its status one last time with jobs to see that it is back to "Running". You have now successfully moved a process from the background to the foreground, stopped it, and resumed it in the background.
Adjust Process Priority with renice
In this step, you will learn how to influence the scheduling priority of a running process. In Linux, the "niceness" of a process determines how much CPU time it gets relative to other processes. The nice value ranges from -20 (highest priority) to +19 (lowest priority). By default, most processes start with a nice value of 0. A higher nice value means the process is "nicer" to other processes, yielding CPU time more readily.
We will adjust the priority of the sleep process you've been working with. To do this, you first need its Process ID (PID). You can find it again using ps and grep.
ps aux | grep sleep
Look for the line corresponding to sleep 300 (not the grep command itself) and note its PID from the second column.
labex 23885 0.0 0.0 7264 868 pts/0 S 11:50 0:00 sleep 300
labex 24101 0.0 0.0 10788 2240 pts/0 S+ 12:15 0:00 grep --color=auto sleep
In this example, the PID is 23885. You must use the PID from your own output in the following commands.
Now, let's check the current nice value (NI) of the process. The ps command with the -o option allows you to specify custom output columns.
ps -o pid,ni,cmd -p <YOUR_PID>
Replace <YOUR_PID> with the actual PID of your sleep process. For example: ps -o pid,ni,cmd -p 23885.
PID NI CMD
23885 5 sleep 300
As expected, the default nice value (NI) is 5.
Now, let's change this value using the renice command. We will increase the nice value to 10, which will lower the process's priority. Regular users can only increase the nice value of their own processes (making them lower priority).
renice -n 10 -p <YOUR_PID>
Again, replace <YOUR_PID> with your process's PID. The command will report the old and new priority.
23885 (process ID) old priority 5, new priority 10
Finally, verify that the change took effect by running the ps command again:
ps -o pid,ni,cmd -p <YOUR_PID>
The output should now show the new nice value.
PID NI CMD
23885 10 sleep 300
You have successfully changed the priority of a running process. This is a useful technique for ensuring that long-running, non-critical background tasks don't interfere with more important foreground work.
Terminate a Process with kill
In this final step, you will learn how to terminate a process. While some processes finish on their own, you'll often need to manually stop a process that is no longer needed, is misbehaving, or was started for temporary purposes, like our sleep command. The primary tool for this is the kill command.
The kill command sends a signal to a specified process. By default, it sends the SIGTERM (terminate) signal, which politely asks the process to shut down, allowing it to perform any cleanup operations before exiting.
You can target a process using either its Process ID (PID) or, for background jobs in your current shell, its job ID. Using the job ID is often more convenient.
First, let's confirm our sleep job is still running.
jobs
You should see the sleep process listed.
[1]+ Running sleep 300 &
Now, use the kill command with the job ID (%1) to terminate it.
kill %1
After you run the command, the shell will likely print a message to the terminal indicating that the job has been terminated. This message might appear immediately or after you press Enter again.
[1]+ Terminated sleep 300
Let's verify that the process is truly gone. Run the jobs command again.
jobs
The command should now produce no output, as there are no more active jobs in this shell session. You can also use ps to double-check.
ps aux | grep sleep
The only line you might see is the grep sleep command itself. The original sleep 300 process is no longer running.
In cases where a process is unresponsive and does not react to the default SIGTERM signal, you can send a more forceful signal, SIGKILL (signal number 9), which terminates the process immediately without giving it a chance to clean up. The command would be kill -9 %1. This should be used as a last resort.
Congratulations! You have now practiced the full lifecycle of basic process management in Linux: starting a process in the background, inspecting it with ps and top, managing it with job control, adjusting its priority, and finally, terminating it.
Summary
In this lab, you learned the fundamental skills for managing and monitoring processes in a Linux environment. You started by running a process in the background using the & operator and viewed its status with the jobs command. You then explored how to inspect all running processes with ps to find details like the Process ID (PID), and how to use top for real-time monitoring of system resource usage and active processes.
Furthermore, you practiced job control by suspending a foreground process with Ctrl-Z, moving it to the background with bg, and bringing it back to the foreground with fg. You also learned how to adjust a process's scheduling priority using renice and how to terminate a process gracefully using the kill command with its PID.



