-
This commit is contained in:
20
hook/nsenter.sh
Normal file
20
hook/nsenter.sh
Normal file
@@ -0,0 +1,20 @@
|
||||
#!/usr/bin/env bash
|
||||
# 在要使用宿主机执行命令时引入该脚本
|
||||
# 示例
|
||||
# # shellcheck disable=SC1091
|
||||
# if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/nsenter.sh "$0"; fi
|
||||
|
||||
_script_path_this=$(realpath "$(ps -p $$ -o args= 2>/dev/null | awk '{print $2}')")
|
||||
_latter="${_script_path_this#/apps/data}"
|
||||
if [[ "$CONTAINER_NAME" == "" ]]; then CONTAINER_NAME=$(grep devices </proc/self/cgroup | grep -Po '[0-9a-z]{64}' | cut -c1-12); fi
|
||||
if [[ "${APPS_DATA}" == "" ]]; then APPS_DATA=$(docker inspect "$CONTAINER_NAME" -f '{{range .Mounts}}{{if eq .Destination "/apps/data"}}{{.Source}}{{end}}{{end}}'); fi
|
||||
if [[ "${APPS_DATA}" == "" ]]; then
|
||||
# echo "nsenter.sh 未挂载 /apps/data 到外部路径"
|
||||
_data_upper_dir=$(docker inspect "$CONTAINER_NAME" -f '{{.GraphDriver.Data.UpperDir}}')
|
||||
_absolute_path="${_data_upper_dir}$_script_path_this"
|
||||
else
|
||||
_absolute_path="${APPS_DATA}$_latter"
|
||||
fi
|
||||
|
||||
nsenter --mount=/host/proc/1/ns/mnt --net=/host/proc/1/ns/net /usr/bin/bash "$_absolute_path"
|
||||
exit
|
||||
39
hook/singleton.sh
Normal file
39
hook/singleton.sh
Normal file
@@ -0,0 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
# shellcheck disable=SC2001
|
||||
# 单例模式
|
||||
# 示例
|
||||
# # shellcheck disable=SC1091
|
||||
# if [[ -d "/host/proc/1/" ]]; then source /apps/gitrce/hook/singleton.sh "$0"; fi
|
||||
|
||||
__singleton_pattern() {
|
||||
# 实现单例模式,确保同一时刻只有一个脚本实例在运行
|
||||
|
||||
_script_path_this=$(realpath "$(ps -p $$ -o args= 2>/dev/null | awk '{print $2}')")
|
||||
|
||||
# 生成 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_file="/apps/pid/$_pid_name"
|
||||
|
||||
# 创建 PID 文件所在的目录
|
||||
mkdir -p "${_pid_file%/*}"
|
||||
|
||||
# 从 PID 文件中读取存储的 PID
|
||||
_pid_data=$(cat "$_pid_file" 2>/dev/null)
|
||||
|
||||
# 根据存储的 PID 获取对应脚本的绝对路径
|
||||
_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
|
||||
# 如果当前脚本路径与存储的脚本路径不同,则更新 PID 文件并继续执行
|
||||
echo "$$" >"$_pid_file"
|
||||
else
|
||||
# 如果相同,则说明已有实例在运行,输出相关信息并退出
|
||||
echo "脚本单例模式运行, 已存在运行实例,当前脚本已退出"
|
||||
echo "_pid_file: $_pid_file"
|
||||
echo "_pid_data: $_pid_data"
|
||||
echo "_script_path_this: $_script_path_this"
|
||||
echo "_script_path_pid : $_script_path_pid"
|
||||
exit 0
|
||||
fi
|
||||
}
|
||||
|
||||
__singleton_pattern
|
||||
9
hook/sleep.sh
Executable file
9
hook/sleep.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
# 需要在执行脚本延时一段时间, 可以调用该脚本...
|
||||
|
||||
_t_sleep=$(shuf -i "$2"-"$3" -n 1)
|
||||
echo "延时:$_t_sleep"
|
||||
sleep "$_t_sleep"
|
||||
|
||||
# 示例
|
||||
# if [[ "$1" == "sleep" ]]; then source /apps/gitrce/hook/sleep.sh "$0" 1 300; fi
|
||||
Reference in New Issue
Block a user