[Bash] Kill command

The kill command sends a signal to a process, usually to terminate the process.

kill PID

This command sends the SIGTERM signal to the process with the given PID.

Common Options:

-l: List all signal names.
-9 or -KILL: Send the SIGKILL signal to forcefully terminate a process.
-15 or -TERM: Send the SIGTERM signal to gracefully terminate a process (default).

# Terminate a process with PID 1234:
kill 1234

# Forcefully terminate a process with PID 1234
kill -9 1234

# List all signal names
kill -l

Pratice

# Find the process IDs and names of the zsh process
$ pgrep -l zsh
1092 zsh

# Find the process IDs of processes for the current user:
pgrep -u $USER
#!/bin/zsh

echo "Enter the process name to terminate: "
read process_name

pids=$(pgrep "$process_name")

# if pids length is 0
if [ -z "$pids" ]; then 
  echo "No process found with the name $process_name."
  exit 1
fi

echo "Found the following PIDs for $process_name: $pids"
echo "Terminating processes..."

for pid in $pid; do
  kill -9 $pid
  echo "Terminated process with PID $pid"
done
  • pgrep "$process_name": Finds all PIDs for the given process name.
  • if [ -z "$pids" ]; then ... fi: Checks if any PIDs were found.
  • for pid in $pids; do ... done: Iterates over each PID and sends the SIGKILL signal to terminate it.
posted @   Zhentiw  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2023-05-24 [HTML 5] Detect visualViewport change
2023-05-24 [React Typescript] useRef with HTML Elements
2023-05-24 [Rust] Option vs Result
2023-05-24 [Rust] Handle Error
2023-05-24 [Rust] Option
2022-05-24 [Next.js] Override the Default Next.js Document
2022-05-24 [Next.js] Override the Default App Component in Next.js
点击右上角即可分享
微信分享提示