Skip to content

Process Management

PotatoScript edited this page Mar 30, 2025 · 1 revision

📚 Process Management in Linux

🌟 1. Introduction to Process Management


💡 A process is a program that is running on your computer. When you open an application (like your web browser or text editor), it becomes a process in your Linux system.

🎮 Think of it like this:

  • 🧑‍💻 Program – A recipe for a delicious cake.
  • 🍰 Process – The cake you’re baking in the oven while following the recipe.
  • 🔄 Managing Processes – Checking on the cake to see how it’s baking and controlling its temperature.

📌 Key Concepts in Process Management:

  • 🧑‍💻 Process ID (PID): A unique number assigned to each running process.
  • 🗃️ Parent and Child Processes: Some processes create other processes. The one creating them is called the parent, and the created ones are the children.

🔧 2. Managing Processes


🏃‍♀️ 1. Viewing Running Processes


💡 To see all the running processes on your system, use the ps command.

ps

📚 Example Output:

PID TTY          TIME CMD
1234 pts/0    00:00:01 bash
5678 pts/0    00:00:00 ps
  • PID: Process ID (unique number for each process)
  • TTY: Terminal associated with the process
  • TIME: Time the process has been running
  • CMD: The command that started the process

💡 To see more detailed information about processes, use:

ps aux

📚 Example Output:

USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root      1234  0.0  0.1 170376  5104 ?        Ss   10:23   0:00 /bin/bash
user      5678  0.0  0.0  24240  1112 pts/0    R+   10:24   0:00 ps aux

🧭 2. Finding a Specific Process


💡 If you're looking for a specific process (like a running web browser), use the grep command to search for it.

ps aux | grep [process_name]

📚 Example:

ps aux | grep firefox

📌 Example Output:

user     1234  5.4  2.1  123456  7890 ?        Sl   10:23   0:02 /usr/lib/firefox/firefox

🛑 3. Stopping (Killing) Processes


💡 Use the kill command to stop a process that is running.

kill [PID]

📚 Example:

kill 1234

If the process doesn’t stop, use the -9 option to force kill:

kill -9 1234

🛠️ 4. Background and Foreground Processes


💡 Processes can run in the foreground (you interact with them directly) or background (they run without interfering with your work).

  • 🔲 Foreground Process: You can see and interact with it.
  • 🔘 Background Process: It runs quietly in the background while you do other things.

🔄 Move a Process to the Background:

To run a process in the background, use the & symbol.

[command] &

📚 Example:

firefox &

⏸️ Move a Process to the Foreground:

If a process is running in the background, you can bring it to the foreground using the fg command.

fg

🔎 5. Monitoring Processes


💡 Use top to see a live, updating list of processes running on your system.

top

📚 Example Output:

top - 10:30:00 up 3 days,  2:04,  2 users,  load average: 0.35, 0.30, 0.25
Tasks: 123 total,   1 running, 122 sleeping,   0 stopped,   0 zombie
%Cpu(s):  4.4 us,  1.3 sy,  0.0 ni, 94.0 id,  0.2 wa,  0.0 hi,  0.1 si,  0.0 st
MiB Mem :   8192.0 total,   1250.0 free,   3145.0 used,   4797.0 buff/cache
MiB Swap:   4096.0 total,   4096.0 free,      0.0 used.   5482.0 avail Mem

You can see:

  • The PID of running processes.
  • CPU and memory usage.
  • Process names and more!

🧑‍💻 3. Advanced Process Management Commands


📅 1. Schedule Processes with cron


💡 Use cron to schedule tasks to run at specific times. It’s like setting an alarm to run a process.

To open the cron scheduler:

crontab -e

📚 Example Cron Job (Run every day at 2:00 AM):

0 2 * * * /path/to/script.sh
  • 0 2 * * * means 2:00 AM every day
  • /path/to/script.sh is the command or script to run

🚀 2. Prioritizing Processes with nice


💡 Use the nice command to set the priority of a process. A process with a low priority uses less CPU, and a process with a high priority gets more CPU.

nice -n [priority] [command]

📚 Example:

nice -n 10 firefox

Higher priority (lower number):

nice -n -10 firefox

3. Using at to Run Processes Once


💡 Use at to run a command once at a specific time.

at 10:30 AM

Then type the command you want to run at 10:30 AM and press Ctrl + D when done.

📚 Example:

at 2:00 PM
echo "Run script" > script_output.txt

Clone this wiki locally