pve_vm_stop
PVE 批量停止实例工具
一键脚本
bash <(curl -sL gitee.com/meimolihan/linux-command_sh/raw/master/pve_vm_stop.sh)
效果预览
补充说明
该脚本用于在 Proxmox VE(PVE)平台上查看并停止 QEMU 虚拟机和 LXC 容器,基于 qm list、pct list、qm stop、pct stop 命令实现,适合需要批量停止多个实例的场景。
功能特点
- 实例列表预览:停止前展示全部 VM 和 CT 实例的详细信息(类型、ID、名称、状态、内存、磁盘、锁)
- 双模式停止:支持 QEMU 虚拟机(
qm stop)和 LXC 容器(pct stop)停止 - 批量停止:支持空格分隔的多个 ID,同时停止多个实例
- 一键全停止:输入
999或传参all自动获取所有实例并全部停止 - 智能跳过:已处于停止状态的实例自动跳过,避免重复操作
- 彩色输出:类型、状态、内存等信息使用不同颜色区分
输出说明
脚本输出包含以下字段:
| 字段 | 说明 |
|---|---|
| 实例列表 | 类型(VM/CT)、ID、名称、状态、内存、磁盘、锁 |
| 停止结果 | 每个实例的停止状态(成功/失败/已停止) |
| 统计汇总 | 停止完成后显示成功和失败数量 |
参数示例
# 交互式模式(先显示列表,再输入ID停止)
bash pve_vm_stop.sh
# 停止单个实例
bash pve_vm_stop.sh 100
# 停止多个实例
bash pve_vm_stop.sh 100 101 236
# 一键停止所有实例
bash pve_vm_stop.sh all
注意事项
- 需要在 PVE 节点上以 root 权限执行
- 已处于停止状态的实例会被自动跳过并提示
- 输入
999等同于停止全部实例 - 实例不存在时会提示错误,不影响其他实例停止
qm stop和pct stop会强制停止实例,可能造成数据丢失,建议先正常关机
脚本源码
#!/bin/bash
list_color_init() {
export gl_hui=$'\033[38;5;59m'
export gl_hong=$'\033[38;5;9m'
export gl_lv=$'\033[38;5;10m'
export gl_huang=$'\033[38;5;11m'
export gl_lan=$'\033[38;5;32m'
export gl_bai=$'\033[38;5;15m'
export gl_zi=$'\033[38;5;13m'
export gl_bufan=$'\033[38;5;14m'
export reset=$'\033[0m'
}
list_color_init
log_info() { echo -e "${gl_lan}[信息]${gl_bai} $*"; }
log_ok() { echo -e "${gl_lv}[成功]${gl_bai} $*"; }
log_warn() { echo -e "${gl_huang}[警告]${gl_bai} $*"; }
log_error() { echo -e "${gl_hong}[错误]${gl_bai} $*" >&2; }
break_end() {
echo -e "${gl_lv}操作完成${gl_bai}"
echo -e "${gl_bai}按任意键继续 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}\c"
read -r -n 1 -s -r -p ""
echo ""
clear
}
column_if_available() {
if command -v column &> /dev/null; then
column -t -s $'\t'
else
cat
fi
}
sleep_fractional() {
local seconds=$1
if sleep "$seconds" 2>/dev/null; then return 0; fi
if command -v perl >/dev/null 2>&1; then perl -e "select(undef, undef, undef, $seconds)"; return 0; fi
if command -v python3 >/dev/null 2>&1; then python3 -c "import time; time.sleep($seconds)"; return 0; fi
if command -v python >/dev/null 2>&1; then python -c "import time; time.sleep($seconds)"; return 0; fi
local int_seconds=$(echo "$seconds" | awk '{print int($1+0.999)}')
sleep "$int_seconds"
}
exit_script() {
echo ""
echo -ne "${gl_hong}感谢使用,再见!${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}\c"
sleep_fractional 0.5
echo -ne "${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}\c"
sleep_fractional 0.6
clear
exit 0
}
parse_qm_list() {
local data
data=$(qm list 2>/dev/null | tail -n +2)
if [ -z "$data" ]; then
return
fi
echo "$data" | while read -r line; do
vmid=$(echo "$line" | awk '{print $1}')
status=$(echo "$line" | awk '{print $3}')
mem=$(echo "$line" | awk '{print $4}')
disk=$(echo "$line" | awk '{print $5}')
lock=$(echo "$line" | awk '{print $6}')
name=$(echo "$line" | sed -E "s/^[ ]*${vmid}[ ]+//;s/[ ]+${status}.*$//" | xargs)
if [[ $status == "running" ]]; then
status_cn="运行中"
st_color="$gl_lv"
else
status_cn="已停止"
st_color="$gl_hong"
fi
echo -e "${gl_huang}VM${reset}\t${gl_lan}${vmid}${reset}\t${gl_bufan}${name}${reset}\t${st_color}${status_cn}${reset}\t${gl_huang}${mem}${reset}\t${gl_zi}${disk}${reset}\t${gl_hui}${lock}${reset}"
done
}
parse_pct_list() {
local raw_output
raw_output=$(pct list 2>/dev/null)
local data
data=$(echo "$raw_output" | tail -n +2)
if [ -z "$data" ]; then
return
fi
echo "$data" | while read -r line; do
ctid=$(echo "$line" | awk '{print $1}')
[[ ! "$ctid" =~ ^[0-9]+$ ]] && continue
status=$(echo "$line" | awk '{print $2}')
if [[ "$status" != "running" && "$status" != "stopped" ]]; then
status=$(echo "$line" | awk '{print $3}')
fi
local fields=($line)
local field_count=${#fields[@]}
if [ $field_count -ge 7 ]; then
mem="${fields[2]}"
disk="${fields[4]}"
lock="${fields[5]}"
name=$(echo "$line" | awk '{for(i=7;i<=NF;i++) printf "%s ", $i; print ""}' | sed 's/ $//')
elif [ $field_count -eq 4 ]; then
mem="${fields[2]}"
disk="-"
lock="-"
name=$(echo "$line" | awk '{for(i=4;i<=NF;i++) printf "%s ", $i; print ""}' | sed 's/ $//')
elif [ $field_count -eq 3 ]; then
mem="-"
disk="-"
lock="-"
name=$(echo "$line" | awk '{for(i=3;i<=NF;i++) printf "%s ", $i; print ""}' | sed 's/ $//')
else
name=$(echo "$line" | sed -E "s/^[ ]*${ctid}[ ]+//" | sed -E "s/^${status}[ ]+//" | sed -E 's/[ ]+[0-9]+[ ]+[0-9.]+[ ]+[^ ]*[ ]*$//')
fi
name=$(echo "$name" | xargs)
[ -z "$name" ] && name="(未命名)"
[[ ! "$mem" =~ ^[0-9]+$ ]] && mem="-"
[[ ! "$disk" =~ ^[0-9.]+$ ]] && disk="-"
[ -z "$lock" ] && lock="-"
if [[ $status == "running" ]]; then
status_cn="运行中"
st_color="$gl_lv"
elif [[ $status == "stopped" ]]; then
status_cn="已停止"
st_color="$gl_hong"
else
status_cn="$status"
st_color="$gl_huang"
fi
echo -e "${gl_lan}CT${reset}\t${gl_lan}${ctid}${reset}\t${gl_bufan}${name}${reset}\t${st_color}${status_cn}${reset}\t${gl_huang}${mem}${reset}\t${gl_zi}${disk}${reset}\t${gl_hui}${lock}${reset}"
done
}
show_all_instance() {
clear
if ! command -v qm &> /dev/null; then
echo -e ""
echo -e "${gl_huang}>>> PVE 全部实例列表(VM虚拟机 + CT容器)${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
log_error "未检测到Proxmox VE环境,请确保脚本在PVE节点上运行"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
break_end
return 1
fi
echo -e "${gl_huang}>>> PVE 全部实例列表(VM虚拟机 + CT容器)${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
{
printf "%s%s\t%s\t%s\t%s\t%s\t%s\t%s%s\n" "$gl_hui" "类型" "ID" "名称" "状态" "内存" "磁盘" "锁" "$reset"
printf "%s%s\t%s\t%s\t%s\t%s\t%s\t%s%s\n" "$gl_hui" "----" "----" "----" "----" "----" "----" "----" "$reset"
parse_qm_list
parse_pct_list
} | column_if_available
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
}
get_all_instance_ids() {
local ids=()
if command -v qm &> /dev/null; then
while IFS= read -r id; do
[ -n "$id" ] && ids+=("$id")
done < <(qm list 2>/dev/null | awk 'NR>1 {print $1}')
fi
if command -v pct &> /dev/null; then
while IFS= read -r id; do
[ -n "$id" ] && ids+=("$id")
done < <(pct list 2>/dev/null | awk 'NR>1 {print $1}')
fi
printf '%s\n' "${ids[@]}" | sort -u | tr '\n' ' '
}
stop_instance() {
local VMID="$1"
local TYPE=""
if qm status "$VMID" &>/dev/null; then
TYPE="QM虚拟机"
local CURRENT_STATUS
CURRENT_STATUS=$(qm status "$VMID" 2>/dev/null | awk '{print $2}')
if [ "$CURRENT_STATUS" = "stopped" ]; then
log_warn "${TYPE} ${VMID} 已经处于停止状态"
return 0
fi
if qm stop "$VMID" &>/dev/null; then
log_ok "${TYPE} ${VMID} 已停止"
return 0
else
log_error "${TYPE} ${VMID} 停止失败"
return 1
fi
elif pct status "$VMID" &>/dev/null; then
TYPE="LXC容器"
local CURRENT_STATUS
CURRENT_STATUS=$(pct status "$VMID" 2>/dev/null | awk '{print $2}')
if [ "$CURRENT_STATUS" = "stopped" ]; then
log_warn "${TYPE} ${VMID} 已经处于停止状态"
return 0
fi
if pct stop "$VMID" &>/dev/null; then
log_ok "${TYPE} ${VMID} 已停止"
return 0
else
log_error "${TYPE} ${VMID} 停止失败"
return 1
fi
else
log_error "实例 ${VMID} 不存在"
return 1
fi
}
# 停止多个实例
stop_instances() {
local ids=("$@")
local success_count=0
local fail_count=0
echo -e "${gl_bufan}————————————————————————————————————————————————${reset}"
for id in "${ids[@]}"; do
if stop_instance "$id"; then
((success_count++))
else
((fail_count++))
fi
done
echo -e "${gl_bufan}————————————————————————————————————————————————${reset}"
echo -e "${gl_lv}成功: ${success_count}${reset} ${gl_hong}失败: ${fail_count}${reset}"
}
main() {
clear
if ! command -v qm &> /dev/null; then
echo -e ""
echo -e "${gl_huang}>>> Proxmox VE 实例停止工具${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
log_error "未检测到Proxmox VE环境,请确保脚本在PVE节点上运行"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
break_end
return 1
fi
echo -e "${gl_zi}>>> Proxmox VE 实例停止工具${reset}"
echo -e "${gl_bufan}————————————————————————————————————————————————${reset}"
local ids_to_stop=()
if [ $# -gt 0 ]; then
if [ "$1" = "all" ]; then
log_info "获取所有实例ID ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
IFS=' ' read -r -a ids_to_stop <<< "$(get_all_instance_ids)"
if [ ${#ids_to_stop[@]} -eq 0 ]; then
log_error "未找到任何实例"
break_end
exit 1
fi
log_info "找到 ${#ids_to_stop[@]} 个实例"
else
ids_to_stop=("$@")
fi
else
clear
if ! command -v qm &> /dev/null; then
echo -e ""
echo -e "${gl_huang}>>> Proxmox VE 实例停止工具${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
log_error "未检测到Proxmox VE环境,请确保脚本在PVE节点上运行"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
break_end
return 1
fi
show_all_instance
echo -e ""
echo -e "${gl_zi}>>> Proxmox VE 实例停止工具${reset}"
echo -e "${gl_bufan}————————————————————————————————————————————————${reset}"
echo -e " • 输入 ${gl_lv}实例ID${gl_bai},多个ID用 ${gl_huang}空格${gl_bai} 分隔"
echo -e " • 输入 ${gl_lv}999${gl_bai} 停止 ${gl_huang}所有实例${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${reset}"
local INPUT
read -r -e -p "$(echo -e "${gl_bai}请输入你的选择(${gl_hong}0${gl_bai} 退出):")" INPUT
if [ "$INPUT" = "0" ]; then
exit_script
fi
if [ "$INPUT" = "999" ]; then
log_info "获取所有实例ID ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}"
IFS=' ' read -r -a ids_to_stop <<< "$(get_all_instance_ids)"
if [ ${#ids_to_stop[@]} -eq 0 ]; then
log_error "未找到任何实例"
break_end
exit 1
fi
log_info "找到 ${#ids_to_stop[@]} 个实例"
else
ids_to_stop=($INPUT)
fi
fi
if [ ${#ids_to_stop[@]} -gt 0 ]; then
stop_instances "${ids_to_stop[@]}"
else
log_warn "没有需要停止的实例"
fi
echo -e "${gl_bufan}————————————————————————————————————————————————${reset}"
break_end
}
main "$@"
创建本地脚本
new_script="new_test.sh"
cat > "$new_script" << 'EOF'
#!/bin/bash
# 粘贴脚本源码
EOF
chmod +x "$new_script" && ./"$new_script" && rm -f "$new_script"