This commit is contained in:
Wang Sen Di
2025-11-25 16:45:15 +08:00
parent 67522a2434
commit b2ef31946b
2 changed files with 43 additions and 34 deletions

View File

@@ -6,34 +6,34 @@
# if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/singleton.sh "$0"; fi # if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/singleton.sh "$0"; fi
__singleton_pattern() { __singleton_pattern() {
# 实现单例模式,确保同一时刻只有一个脚本实例在运行 # 实现单例模式,确保同一时刻只有一个脚本实例在运行
_script_path_this=$(realpath "$(ps -p $$ -o args= 2>/dev/null | awk '{print $2}')") _script_path_this=$(realpath "$(ps -p $$ -o args= 2>/dev/null | awk '{print $2}')")
# 生成 PID 文件的路径 # 生成 PID 文件的路径
_pid_name="$(echo "$_script_path_this" | sed 's:^/::; s/\//#/g; s/\.sh$/.pid/')" # 还原 echo "$result" | sed 's:^:/:' | sed 's/#/\//g; s/\.pid$/.sh/' _pid_name="$(echo "$_script_path_this" | sed 's:^/::; s/\//#/g; s/\.sh$/.pid/')" # 还原 echo "$result" | sed 's:^:/:' | sed 's/#/\//g; s/\.pid$/.sh/'
_pid_file="/apps/pid/$_pid_name" _pid_file="/apps/pid/$_pid_name"
# 创建 PID 文件所在的目录 # 创建 PID 文件所在的目录
mkdir -p "${_pid_file%/*}" mkdir -p "${_pid_file%/*}"
# 从 PID 文件中读取存储的 PID # 从 PID 文件中读取存储的 PID
_pid_data=$(cat "$_pid_file" 2>/dev/null) _pid_data=$(cat "$_pid_file" 2>/dev/null)
# 根据存储的 PID 获取对应脚本的绝对路径 # 根据存储的 PID 获取对应脚本的绝对路径
_script_path_pid=$(realpath "$(ps -p "$_pid_data" -o args= 2>/dev/null | awk '{print $2}')" 2>/dev/null) _script_path_pid=$(realpath "$(ps -p "$_pid_data" -o args= 2>/dev/null | awk '{print $2}')" 2>/dev/null)
if [[ "$_script_path_this" != "$_script_path_pid" ]]; then if [[ "$_script_path_this" != "$_script_path_pid" ]]; then
# 如果当前脚本路径与存储的脚本路径不同,则更新 PID 文件并继续执行 # 如果当前脚本路径与存储的脚本路径不同,则更新 PID 文件并继续执行
echo "$$" >"$_pid_file" echo "$$" >"$_pid_file"
else else
# 如果相同,则说明已有实例在运行,输出相关信息并退出 # 如果相同,则说明已有实例在运行,输出相关信息并退出
echo "脚本单例模式运行, 已存在运行实例,当前脚本已退出" echo "脚本单例模式运行, 已存在运行实例,当前脚本已退出"
echo "_pid_file: $_pid_file" echo "_pid_file: $_pid_file"
echo "_pid_data: $_pid_data" echo "_pid_data: $_pid_data"
echo "_script_path_this: $_script_path_this" echo "_script_path_this: $_script_path_this"
echo "_script_path_pid : $_script_path_pid" echo "_script_path_pid : $_script_path_pid"
exit 0 exit 0
fi fi
} }
__singleton_pattern __singleton_pattern

View File

@@ -3,20 +3,29 @@
if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/singleton.sh "$0"; fi if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/singleton.sh "$0"; fi
_owner_id=50000 __main() {
_chain="output_netflow_owner_${_owner_id}" :
update-alternatives --set iptables /usr/sbin/iptables-legacy 2>/dev/null
for cmd in iptables ip6tables; do _owner_id=50000
$cmd -t mangle -N "$_chain" 2>/dev/null || true _chain="output_netflow_owner_${_owner_id}"
if ! $cmd -t mangle -C OUTPUT -m owner --gid-owner "$_owner_id" -j "$_chain" 2>/dev/null; then for cmd in iptables ip6tables; do
$cmd -t mangle -A OUTPUT -m owner --gid-owner "$_owner_id" -j "$_chain" $cmd -t mangle -N "$_chain" 2>/dev/null || true
fi
if ! $cmd -t mangle -C "$_chain" -j RETURN 2>/dev/null; then if ! $cmd -t mangle -C OUTPUT -m owner --gid-owner "$_owner_id" -j "$_chain" 2>/dev/null; then
$cmd -t mangle -A "$_chain" -j RETURN $cmd -t mangle -A OUTPUT -m owner --gid-owner "$_owner_id" -j "$_chain"
fi fi
done
if ! $cmd -t mangle -C "$_chain" -j RETURN 2>/dev/null; then
$cmd -t mangle -A "$_chain" -j RETURN
fi
$cmd -t mangle -L OUTPUT -v -n -x
done
}
__main
__help() { __help() {
cat >/dev/null <<-'EOF' cat >/dev/null <<-'EOF'