Skip to content

Commit 2919ce1

Browse files
authored
pve-tool: execute.sh by @jeroenzwart (#7708)
1 parent 38cf6b2 commit 2919ce1

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

frontend/public/json/execute.json

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "PVE LXC Execute Command",
3+
"slug": "lxc-execute",
4+
"categories": [
5+
1
6+
],
7+
"date_created": "2025-09-18",
8+
"type": "pve",
9+
"updateable": false,
10+
"privileged": false,
11+
"interface_port": null,
12+
"documentation": null,
13+
"website": null,
14+
"logo": "https://cdn.jsdelivr.net/gh/selfhst/icons/webp/proxmox.webp",
15+
"config_path": "",
16+
"description": "This script allows administrators to execute a custom command inside one or multiple LXC containers on a Proxmox VE node. Containers can be selectively excluded via an interactive checklist. If a container is stopped, the script will automatically start it, run the command, and then shut it down again. Only Debian and Ubuntu based containers are supported.",
17+
"install_methods": [
18+
{
19+
"type": "default",
20+
"script": "tools/pve/execute.sh",
21+
"resources": {
22+
"cpu": null,
23+
"ram": null,
24+
"hdd": null,
25+
"os": null,
26+
"version": null
27+
}
28+
}
29+
],
30+
"default_credentials": {
31+
"username": null,
32+
"password": null
33+
},
34+
"notes": [
35+
{
36+
"text": "Execute within the Proxmox shell.",
37+
"type": "info"
38+
},
39+
{
40+
"text": "Non-Debian/Ubuntu containers will be skipped automatically.",
41+
"type": "info"
42+
},
43+
{
44+
"text": "Stopped containers will be started temporarily to run the command, then shut down again.",
45+
"type": "warning"
46+
}
47+
]
48+
}

tools/pve/execute.sh

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright (c) 2021-2025 community-scripts ORG
4+
# Author: jeroenzwart
5+
# License: MIT
6+
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
7+
8+
function header_info() {
9+
clear
10+
cat <<"EOF"
11+
______ __ __ _ ________
12+
/ ____/ _____ _______ __/ /____ / / | |/ / ____/
13+
/ __/ | |/_/ _ \/ ___/ / / / __/ _ \ / / | / /
14+
/ /____> </ __/ /__/ /_/ / /_/ __/ / /___/ / /___
15+
/_____/_/|_|\___/\___/\__,_/\__/\___/ /_____/_/|_\____/
16+
17+
EOF
18+
}
19+
set -eEuo pipefail
20+
BL=$(echo "\033[36m")
21+
RD=$(echo "\033[01;31m")
22+
CM='\xE2\x9C\x94\033'
23+
GN=$(echo "\033[1;92m")
24+
CL=$(echo "\033[m")
25+
header_info
26+
echo "Loading..."
27+
whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC Execute" --yesno "This will execute a command inside selected LXC Containers. Proceed?" 10 58
28+
NODE=$(hostname)
29+
EXCLUDE_MENU=()
30+
MSG_MAX_LENGTH=0
31+
while read -r TAG ITEM; do
32+
OFFSET=2
33+
((${#ITEM} + OFFSET > MSG_MAX_LENGTH)) && MSG_MAX_LENGTH=${#ITEM}+OFFSET
34+
EXCLUDE_MENU+=("$TAG" "$ITEM " "OFF")
35+
done < <(pct list | awk 'NR>1')
36+
excluded_containers=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Containers on $NODE" --checklist "\nSelect containers to skip from executing:\n" \
37+
16 $((MSG_MAX_LENGTH + 23)) 6 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"')
38+
39+
if [ $? -ne 0 ]; then
40+
exit
41+
fi
42+
43+
44+
read -r -p "Enter here command for inside the containers: " custom_command
45+
46+
header_info
47+
echo "One moment please...\n"
48+
49+
function execute_in() {
50+
container=$1
51+
name=$(pct exec "$container" hostname)
52+
echo -e "${BL}[Info]${GN} Execute inside${BL} ${name}${GN} with output: ${CL}"
53+
pct exec "$container" -- bash -c "${custom_command}" | tee
54+
}
55+
56+
for container in $(pct list | awk '{if(NR>1) print $1}'); do
57+
if [[ " ${excluded_containers[@]} " =~ " $container " ]]; then
58+
echo -e "${BL}[Info]${GN} Skipping ${BL}$container${CL}"
59+
else
60+
os=$(pct config "$container" | awk '/^ostype/ {print $2}')
61+
if [ "$os" != "debian" ] && [ "$os" != "ubuntu" ]; then
62+
echo -e "${BL}[Info]${GN} Skipping ${name} ${RD}$container is not Debian or Ubuntu ${CL}"
63+
continue
64+
fi
65+
66+
status=$(pct status "$container")
67+
template=$(pct config "$container" | grep -q "template:" && echo "true" || echo "false")
68+
if [ "$template" == "false" ] && [ "$status" == "status: stopped" ]; then
69+
echo -e "${BL}[Info]${GN} Starting${BL} $container ${CL}"
70+
pct start "$container"
71+
echo -e "${BL}[Info]${GN} Waiting For${BL} $container${CL}${GN} To Start ${CL}"
72+
sleep 5
73+
execute_in "$container"
74+
echo -e "${BL}[Info]${GN} Shutting down${BL} $container ${CL}"
75+
pct shutdown "$container" &
76+
elif [ "$status" == "status: running" ]; then
77+
execute_in "$container"
78+
fi
79+
fi
80+
done
81+
82+
wait
83+
84+
echo -e "${GN} Finished, execute command inside selected containers. ${CL} \n"

0 commit comments

Comments
 (0)