← Back to Bash Course | Chapter 11: Process Management | Lesson 12 of 12

Processes को Kill करना

kill command एक process को एक signal भेजती है, आमतौर पर इसे रुकने के लिए कहते हुए, और एक process इस request पर अलग तरीके से respond कर सकता है यह इस पर depend करता है कि कौन सा signal भेजा गया था।
Syntax
bash
kill process_id
kill -0 process_id
kill -9 process_id

Sending the Default Signal

बिना किसी signal name के kill pid SIGTERM भेजता है, एक polite termination request जिसे एक process trap कर सकता है और actually exit होने से पहले respond कर सकता है, एक forceful kill के विपरीत।

उदाहरण: Sending the Default Signal

bash
#!/bin/bash
sleep 5 &
pid=$!
kill "$pid"
wait "$pid" 2>/dev/null
echo "background sleep was asked to terminate, wait returned status $?"

Checking Before You Kill

kill -0 pid कोई असली signal नहीं भेजता लेकिन यह check करता है कि process मौजूद है और signalable है या नहीं, एक normal exit status return करते हुए; यह एक already-finished job को kill करने की कोशिश करने से होने वाली errors से बचाता है।

उदाहरण: Checking Before You Kill

bash
#!/bin/bash
sleep 0.2 &
pid=$!
if kill -0 "$pid" 2>/dev/null; then
    echo "process $pid is still running"
fi
wait "$pid"
if ! kill -0 "$pid" 2>/dev/null; then
    echo "process $pid is no longer running"
fi

Forceful Termination with SIGKILL

kill -9 pid (SIGKILL) target process द्वारा caught, blocked, या ignored नहीं किया जा सकता, इसलिए यह हमेशा इसे immediately terminate करता है, लेकिन process को अपना cleanup code run करने का कोई opportunity नहीं मिलता।

Note: SIGKILL को उन processes के लिए reserve करें जो एक normal SIGTERM request को ignore करते हैं, क्योंकि यह process जो भी cleanup run करना चाहता हो उसे skip करता है।

उदाहरण: Forceful Termination with SIGKILL

bash
#!/bin/bash
sleep 5 &
pid=$!
kill -9 "$pid"
wait "$pid" 2>/dev/null
status=$?
echo "exit status after SIGKILL: $status"

Understanding Signal-Based Exit Codes

जब एक process normally exit होने के बजाय एक signal से terminate होता है, इसका reported exit status conventionally 128 plus signal के number होता है (उदाहरण के लिए, SIGTERM 15 है, इसलिए एक terminated process अक्सर 143 report करता है)।

ध्यान दें यहां $BASHPID (असली subshell की PID) $$ के बजाय उपयोग किया गया है, क्योंकि $$ हमेशा original top-level shell को refer करता है भले ही एक subshell के अंदर हो।

Warning: एक subshell के अंदर से $$ के बजाय $BASHPID को SIGTERM भेजना पूरे script को terminate कर देगा, सिर्फ subshell को नहीं।

उदाहरण: Understanding Signal-Based Exit Codes

bash
#!/bin/bash
( kill -TERM $BASHPID ) &
sub_pid=$!
wait "$sub_pid"
echo "the subshell that sent itself SIGTERM reported status: $?"
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मान लेना कि kill हमेशा एक process को instantly forcefully terminate करता है; default रूप से यह SIGTERM भेजता है, एक polite request जिसे target catch कर सकता है और honor करने से पहले ignore या clean up कर सकता है।
  2. यह भूल जाना कि kill -9 (SIGKILL) को caught, blocked, या ignored नहीं किया जा सकता, जो powerful है लेकिन इसका मतलब यह भी है कि target को clean up करने का कोई chance नहीं मिलता।
  3. एक background job को kill करने की कोशिश करने से पहले यह check न करना कि वह already खत्म हो गई है, जो "no such process" जैसा एक error produce करता है।
चैप्टर सारांश
  • kill pid default रूप से SIGTERM भेजता है, एक request जिसे एक well-behaved process exit होने से पहले catch और act कर सकता है।
  • kill -9 pid (SIGKILL) unconditionally terminate करता है और target द्वारा caught या ignored नहीं किया जा सकता।
  • Signal N से killed एक process conventionally अपने parent को 128 + N का exit status report करता है।
  • एक PID अभी भी running है या नहीं यह check करना (जैसे ps -p या kill -0 से) इसे signal करने से पहले "no such process" errors से बचाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.