0Pricing
Linux Command Line Mastery · บทเรียน

การจัดการกระบวนการ: `kill`, `bg`, `fg`

เรียนรู้การยุติ หยุดชั่วคราว และดำเนินกระบวนการต่อจากบรรทัดคำสั่งอย่างมีประสิทธิภาพ

การจัดการกระบวนการ: `kill`, `bg`, `fg` เป็นบทเรียน Linux Command Line Mastery ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Linux Command Line Mastery และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Introduction to Process Control

In Linux, every running program is called a process. Managing these processes is essential for system efficiency and troubleshooting.

This lesson teaches you how to take control of processes: suspending them, sending them to the background, bringing them forward, and even terminating them when they misbehave.

The `kill` Command Basics

The kill command is used to send signals to processes. Its primary use is to terminate processes.

To use kill, you need the Process ID (PID) of the target process. You can find PIDs using commands like ps (covered in a previous lesson).

ps aux | grep sleep

Graceful Termination: `SIGTERM`

By default, kill sends the SIGTERM (signal 15) signal. This is a polite request for a process to terminate.

A well-behaved program will catch SIGTERM, perform any necessary cleanup (like saving files), and then exit gracefully.

sleep 30 &
# Use 'ps' to find the PID of 'sleep 30'
# Example: kill <PID_OF_SLEEP>

Forceful Termination: `SIGKILL` (-9)

Sometimes, a process might ignore SIGTERM, becoming unresponsive or 'frozen'. In such cases, you need to use a stronger signal: SIGKILL (signal 9).

SIGKILL immediately terminates the process without allowing it to clean up. Use it as a last resort!

sleep 60 &
# Use 'ps' to find the PID of 'sleep 60'
# Example: kill -9 <PID_OF_SLEEP>

Suspending Processes with Ctrl+Z

You can temporarily pause a running foreground process using the keyboard shortcut Ctrl+Z.

This sends a SIGSTOP signal, suspending the process and returning control to your shell. The process is still in memory but not executing.

sleep 120
# Type 'sleep 120' and then press Ctrl+Z
# You will see '[1]+  Stopped  sleep 120'

Listing Jobs with `jobs`

When you suspend a process or send it to the background, it becomes a 'job'. You can list all current jobs managed by your shell using the jobs command.

Each job is assigned a job number, which is useful for referring to it with other commands.

sleep 30
# Press Ctrl+Z
jobs

Backgrounding a Job: `bg`

A suspended process isn't running. To make it run in the background (without occupying your terminal), use the bg command.

You can refer to the job by its job number (e.g., bg %1). The process will continue running silently.

sleep 60
# Press Ctrl+Z
jobs
bg %1
jobs

Foregrounding a Job: `fg`

If you have a process running in the background or a suspended one, you can bring it back to the foreground to interact with it using the fg command.

Like bg, you specify the job number (e.g., fg %1) to bring that specific job to the front.

sleep 60 &
jobs
fg %1
# Press Ctrl+C to stop it or Ctrl+Z to suspend it again.

Combining Process Control

These commands work together to give you full control over processes:

  • Start a task: nano
  • Suspend it: Ctrl+Z
  • Move to background: bg %1
  • Start another task: sleep 100
  • Suspend it: Ctrl+Z
  • List all jobs: jobs
  • Bring nano to foreground: fg %1
  • Terminate nano: Ctrl+C
  • Kill sleep: kill %2

Quick Check

You've started a program, and it's completely unresponsive. You can't even close it with Ctrl+C. You know its Process ID (PID).

What is the most effective command to immediately terminate this stubborn process?

Recap: Process Control

Great job! You've learned how to manage processes effectively:

  • kill: Send signals to terminate processes.
  • kill -9: Forcefully terminate a process.
  • Ctrl+Z: Suspend a foreground process.
  • jobs: List background and suspended jobs.
  • bg: Send a suspended job to the background.
  • fg: Bring a background job to the foreground.

These commands are crucial for maintaining control over your Linux environment.

คำถามที่พบบ่อย

บทเรียน “การจัดการกระบวนการ: `kill`, `bg`, `fg`” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การจัดการกระบวนการ: `kill`, `bg`, `fg`” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Linux Command Line Mastery ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Linux Command Line Mastery มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการกระบวนการ: `kill`, `bg`, `fg`”

เรียนรู้การยุติ หยุดชั่วคราว และดำเนินกระบวนการต่อจากบรรทัดคำสั่งอย่างมีประสิทธิภาพ คุณปฏิบัติ Linux Command Line Mastery ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Linux Command Line Mastery หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Linux Command Line Mastery บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การจัดการกระบวนการ: `kill`, `bg`, `fg`” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Linux Command Line Mastery นี้ได้ไหม

ได้ บทเรียน Linux Command Line Mastery ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ทำความเข้าใจกระบวนการ: `ps`, `top`, `htop`
  2. การจัดการกระบวนการ: `kill`, `bg`, `fg`
  3. การตรวจสอบทรัพยากรระบบ: `df`, `du`, `free`
  4. ลำดับความสำคัญของโพรเซสด้วย nice, renice และสัญญาณ
← กลับไปที่ Linux Command Line Mastery