first commit

This commit is contained in:
2021-08-29 00:02:47 +08:00
commit 01e8b33396
52 changed files with 4404 additions and 0 deletions

67
常用脚本/shell/check_net.sh Executable file
View File

@@ -0,0 +1,67 @@
#!/bin/bash
#=========================================
# Filename : check_net.sh
# Author : Colben
# Create : 2017-12-04 11:04
#=========================================
PROCS=()
function Quit {
[ -n "$1" ] && echo -e "\033[31;1mERROR: $1 !\033[0m"
exit 1
}
function StopPing {
for pid in ${!PROCS[@]}; do
kill $pid
done
Quit
}
function PingAddr {
local oldState=
local newState=
local alterTime=
while :; do
ping -w4 -c2 -q $1 > /dev/null 2>&1
newState=$?
[ "$oldState" = "$newState" ] || alterTime="$(date +'%H:%M')"
oldState=$newState
if [ '0' = "$newState" ]; then
echo "\033[32;1m connected\033[0m on $alterTime." > $1
sleep 8
else
echo "\033[31;1m lost\033[0m on $alterTime!" > $1
sleep 4
fi
done
}
# start
[ 0 -eq $# ] && Quit "No hosts found"
mkdir -p /tmp/check_net || Quit "create dir failed"
cd /tmp/check_net || Quit "change dir failed"
trap 2 3 15 "StopPing"
for addr in $@; do
PingAddr $addr &
PROCS[$!]="$addr"
done
while :; do
for pid in ${!PROCS[@]}; do
if [ ! -f /proc/$pid/stat ]; then
PingAddr ${PROCS[$pid]} &
PROC[$!]=PROC[$pid]
unset PROC[$pid]
fi
done
clear
for f in $(ls); do
echo -e "$f:$(cat $f)"
done
sleep 4
done

55
常用脚本/shell/install.sh Executable file
View File

@@ -0,0 +1,55 @@
#!/bin/bash
# usage
USAGE="Usage: $0"
# installation processes
PROCESSES='
CheckFun1
CheckFun2
InitFun1
InitFun2
'
# report error and quit
function Quit {
[ -n "$1" ] && echo "$1!" > /dev/stderr
echo -e "\033[31;1mInstallaion failed!\033[0m\n"
exit 1
}
function CheckFun1 {
echo ''
}
function CheckFun2 {
echo ''
}
function InitFun1 {
echo ''
}
function InitFun2 {
echo ''
}
# execute every function in PROCESSES
function Main {
for process in $PROCESSES; do
echo -e "\n\033[33;1m- $func ...\033[0m"
$process
done
echo -e "\033[32;1mInstall successfully!\033[0m\n"
}
# start here
# check root privilege
[ 0 -ne $UID ] && Quit "Can't run without ROOT"
# check arguments
[ 0 -ne $# ] && Quit "$USAGE"
# cd to script dir
cd $(dirname $0) || Quit
# install
Main

View File

@@ -0,0 +1,19 @@
#!/bin/bash
SERVICES=(
'rabbitmq-server'
'redis-server'
'mongod'
'mysql'
'tomcat7'
'nginx'
)
DAEMONS=(
'/a/b/c -f -g'
'd -e iiii'
'/j/k'
'llll'
'/mm/nn -a -b -c'
)

180
常用脚本/shell/rc.all Executable file
View File

@@ -0,0 +1,180 @@
#!/bin/bash
function Quit {
[ -n "$1" ] && echo -e "\n\033[31;1mERROR: $1\033[0m!\n"
exit 3
}
function Spreadout {
local i=
local service=
local daemon=
echo "System ${ROOT_DIR##*/} processes ..."
for service in ${SERVICES[@]}; do
echo "Service: \"$service\""
done
for i in ${!DAEMONS[@]}; do
daemon="${DAEMONS[i]}"
echo "Daemon: \"$daemon\""
done
exit 0
}
function ShowState {
local name=$1
local err=$2
local total=$3
local style=$4
if [ '0' = "$total" ]; then
echo -e "$name \033[32${style}mnot found\033[0m ..."
return 254
elif [ '0' = "$err" ]; then
echo -e "$name \033[32${style}mrunning\033[0m ..."
return 0
elif [ -z "$total" -o "$total" = "$err" ]; then
echo -e "$name \033[33${style}mstopped\033[0m!"
return 1
else
echo -e "$name \033[31${style}merror\033[0m!!"
return 255
fi
}
function HandleServices {
local service=
for service in ${SERVICES[@]}; do
echo "===> ${1}ing \"$service\" ..."
systemctl $1 $service
sleep 0.4
done
}
function HandleDaemons {
local i=
local daemon=
for i in ${!DAEMONS[@]}; do
daemon="${DAEMONS[i]}"
echo "===> ${1}ing \"$daemon\" ..."
$WATCH_PROC $1 $daemon
sleep 0.4
done
}
function CheckServices {
local err=0
local service=
for service in ${SERVICES[@]}; do
systemctl status $service > /dev/null 2>/dev/null
ShowState "Service: \"$service\"" $? || let "err=1+$err"
done
ShowState Services $err ${#SERVICES[@]} ';1;3'
return $?
}
function CheckDaemons {
local err=0
local daemon=
local daemon_name=
for i in ${!DAEMONS[@]}; do
daemon="${DAEMONS[i]}"
daemon_name="$(basename ${daemon%% *})"
pgrep -x ${daemon_name:0:15} > /dev/null 2>/dev/null
ShowState "Daemon: \"$daemon\"" $? || let "err=1+$err"
done
ShowState Daemons $err ${#DAEMONS[@]} ';1;3'
return $?
}
function CheckSystem {
local i=
local err=0
local ret=0
local output=
local items=2
for i in CheckServices CheckDaemons; do
output=$($i)
ret=$?
if [ 254 -eq $ret ]; then
let "items=${items}-1"
elif [ 0 -lt $ret ]; then
let "err=1+$err"
fi
if [ -z "$1" ]; then
echo -e "$output" | tail -1
else
echo -e "$output"
fi
done
ShowState "System \"${ROOT_DIR##*/}\"" $err $items ';1'
exit $?
}
# start here
type pgrep > /dev/null || exit 1
cd $(dirname $0)
. processes || Quit 'File "processes" not found'
[ -x rc.pwatch ] || Quit 'Executalbe process not found'
WATCH_PROC="$(pwd)/rc.pwatch"
cd .. && ROOT_DIR=$(pwd) || Quit 'Change dir failed'
case $* in
'')
CheckSystem
;;
status)
CheckSystem detail
;;
start)
for check in CheckServices CheckDaemons; do
$check > /dev/null 2>/dev/null
check_ret=$?
[ 254 -eq $check_ret ] && continue
if [ 0 -ne $check_ret ]; then
Handle${check#Check} start
output=$($check)
if [ 0 -ne $? ]; then
echo -e "$output"
Quit "${check#Check} start failed"
fi
fi
done
CheckSystem
;;
stop)
for check in CheckDaemons CheckServices; do
$check > /dev/null 2>/dev/null
check_ret=$?
[ 254 -eq $check_ret ] && continue
[ 1 -ne $check_ret ] && Handle${check#Check} stop
done
CheckSystem
;;
startservice)
CheckServices > /dev/null 2>/dev/null
check_ret=$?
if [ 254 -eq $check_ret ]; then
CheckSystem
elif [ 0 -ne $check_ret ]; then
HandleServices start
CheckSystem
fi
;;
stopdaemon)
CheckDaemons > /dev/null 2>/dev/null
check_ret=$?
if [ 254 -eq $check_ret ]; then
CheckSystem
elif [ 1 -ne $check_ret ]; then
HandleDaemons stop
CheckSystem
fi
;;
list)
Spreadout
;;
*)
echo "Usage: $0 [status|start[service]|stop[daemon]|list]"
exit 3
;;
esac

184
常用脚本/shell/rc.all_mul Executable file
View File

@@ -0,0 +1,184 @@
#!/bin/bash
function Quit {
[ -n "$1" ] && echo -e "\n\033[31;1mERROR: $1\033[0m!\n"
exit 3
}
function Spreadout {
local i=
local service=
local daemon=
echo "System ${ROOT_DIR##*/} processes ..."
for service in ${SERVICES[@]}; do
echo "Service: \"$service\""
done
for i in ${!DAEMONS[@]}; do
daemon="${DAEMONS[i]}"
echo "Daemon: \"$daemon\""
done
exit 0
}
function ShowState {
local name=$1
local err=$2
local total=$3
local style=$4
if [ '0' = "$total" ]; then
echo -e "$name \033[32${style}mnot found\033[0m ..."
return 254
elif [ '0' = "$err" ]; then
echo -e "$name \033[32${style}mrunning\033[0m ..."
return 0
elif [ -z "$total" -o "$total" = "$err" ]; then
echo -e "$name \033[33${style}mstopped\033[0m!"
return 1
else
echo -e "$name \033[31${style}merror\033[0m!!"
return 255
fi
}
function HandleServices {
local service=
for service in ${SERVICES[@]}; do
echo "===> ${1}ing \"$service\" ..."
systemctl $1 $service
sleep 0.4
done
}
function HandleDaemons {
local i=
local daemon=
for i in ${!DAEMONS[@]}; do
daemon="${DAEMONS[i]}"
echo "===> ${1}ing \"$daemon\" ..."
$WATCH_PROC $1 $daemon
sleep 0.4
done
}
function CheckServices {
local err=0
local service=
for service in ${SERVICES[@]}; do
systemctl status $service > /dev/null 2>/dev/null
ShowState "Service: \"$service\"" $? || let "err=1+$err"
done
ShowState Services $err ${#SERVICES[@]} ';1;3'
return $?
}
function CheckDaemons {
local err=0
local daemon=
local daemon_cmd=
local output=
local output_watch=
for i in ${!DAEMONS[@]}; do
daemon="${DAEMONS[i]}"
daemon_cmd="${daemon#$(dirname ${daemon%% *})/}"
output=$(pgrep -f "$daemon_cmd$")
output_watch=(pgrep -f "$(basename $WATCH_PROC) +start +[^ ]*\<$daemon_cmd$")
[ -n "$output" -a "$output" != "$output_watch" ]
ShowState "Daemon: \"$daemon\"" $? || let "err=1+$err"
done
ShowState Daemons $err ${#DAEMONS[@]} ';1;3'
return $?
}
function CheckSystem {
local i=
local err=0
local ret=0
local output=
local items=2
for i in CheckServices CheckDaemons; do
output=$($i)
ret=$?
if [ 254 -eq $ret ]; then
let "items=${items}-1"
elif [ 0 -lt $ret ]; then
let "err=1+$err"
fi
if [ -z "$1" ]; then
echo -e "$output" | tail -1
else
echo -e "$output"
fi
done
ShowState "System \"${ROOT_DIR##*/}\"" $err $items ';1'
exit $?
}
# start here
type pgrep > /dev/null || exit 1
cd $(dirname $0)
. processes || Quit 'File "processes" not found'
[ -x rc.pwatch ] || Quit 'Executalbe process not found'
WATCH_PROC="$(pwd)/rc.pwatch"
cd .. && ROOT_DIR=$(pwd) || Quit 'Change dir failed'
case $* in
'')
CheckSystem
;;
status)
CheckSystem detail
;;
start)
for check in CheckServices CheckDaemons; do
$check > /dev/null 2>/dev/null
check_ret=$?
[ 254 -eq $check_ret ] && continue
if [ 0 -ne $check_ret ]; then
Handle${check#Check} start
output=$($check)
if [ 0 -ne $? ]; then
echo -e "$output"
Quit "${check#Check} start failed"
fi
fi
done
CheckSystem
;;
stop)
for check in CheckDaemons CheckServices; do
$check > /dev/null 2>/dev/null
check_ret=$?
[ 254 -eq $check_ret ] && continue
[ 1 -ne $check_ret ] && Handle${check#Check} stop
done
CheckSystem
;;
startservice)
CheckServices > /dev/null 2>/dev/null
check_ret=$?
if [ 254 -eq $check_ret ]; then
CheckSystem
elif [ 0 -ne $check_ret ]; then
HandleServices start
CheckSystem
fi
;;
stopdaemon)
CheckDaemons > /dev/null 2>/dev/null
check_ret=$?
if [ 254 -eq $check_ret ]; then
CheckSystem
elif [ 1 -ne $check_ret ]; then
HandleDaemons stop
CheckSystem
fi
;;
list)
Spreadout
;;
*)
echo "Usage: $0 [status|start[service]|stop[daemon]|list]"
exit 3
;;
esac

50
常用脚本/shell/rc.pwatch Executable file
View File

@@ -0,0 +1,50 @@
#!/bin/bash
type pgrep > /dev/null || exit 1
[ 2 -gt $# ] && echo -e "\tUsage:$0 {start|stop} {cmd}\n" && exit 1
cd $(dirname $0)/../
WATCH_LOG=/var/log/watch_proc.log
WATCH_NAME=$(basename $0)
OPERATION=$1
shift
PROC_CMD="$*"
PROC_NAME=$(basename ${PROC_CMD%% *})
function StopProc {
local cpids=$(pgrep -P $1)
local cpid=
printf "%${2}s|-$1\n"
[ -f /proc/$1/stat ] && kill $1
local n=$(expr 2 + $2)
for cpid in $cpids; do
StopProc "$cpid" $n
done
}
case $OPERATION in
start)
pid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>")
pid=$(echo -e "$pid"|grep -v "^$$$")
[ -n "$pid" ] && echo "WATCH on \"$PROC_CMD\" is runing with PID:$pid" && exit 2
while :; do
$PROC_CMD > /dev/null 2>/dev/null &
pid=$!
while sleep 4; do [ -f /proc/$pid/stat ] || break; done
echo "$(date +'%F %T') \"$PROC_CMD\" quit exceptionally!" >> $WATCH_LOG
StopProc "$pid" 0 >> $WATCH_LOG
echo "$(date +'%F %T') \"$PROC_CMD\" restart." >> $WATCH_LOG
done > /dev/null 2>/dev/null & ;;
stop)
ppid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>")
[ -z "$ppid" ] && echo "No WATCH on \"$PROC_CMD\" !" && exit 1
pid=$(pgrep -lP $ppid|grep -v '^[0-9]\+ \+sleep$'|awk '{print $1}')
[ -z "$pid" ] && echo "No process: \"$PROC_CMD\"" && kill -9 $ppid && exit 0
kill -9 $ppid
echo "$(date +'%F %T') \"$PROC_CMD\" quit manually." >> $WATCH_LOG
StopProc "$pid" 0
exit 0 ;;
*)
echo -e "\tUsage:$0 start|stop cmd argv ...\n" ;;
esac

View File

@@ -0,0 +1,51 @@
#!/bin/bash
type pgrep > /dev/null || exit 1
[ 2 -gt $# ] && echo -e "\tUsage:$0 {start|stop} {cmd}\n" && exit 1
cd $(dirname $0)/../
WATCH_LOG=/var/log/watch_proc.log
WATCH_NAME=$(basename $0)
OPERATION=$1
shift
PROC_CMD="$*"
PROC_NAME=$(basename ${PROC_CMD%% *})
PROC_ARGS="${PROC_CMD#*$PROC_NAME}"
function StopProc {
local cpids=$(pgrep -P $1)
local cpid=
printf "%${2}s|-$1\n"
[ -f /proc/$1/stat ] && kill $1
local n=$(expr 2 + $2)
for cpid in $cpids; do
StopProc "$cpid" $n
done
}
case $OPERATION in
start)
pid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>$PROC_ARGS$")
pid=$(echo -e "$pid"|grep -v "^$$$")
[ -n "$pid" ] && echo "WATCH on \"$PROC_CMD\" is runing with PID:$pid" && exit 2
while :; do
$PROC_CMD > /dev/null 2>/dev/null &
pid=$!
while sleep 4; do [ -f /proc/$pid/stat ] || break; done
echo "$(date +'%F %T') \"$PROC_CMD\" quit exceptionally!" >> $WATCH_LOG
StopProc "$pid" 0 >> $WATCH_LOG
echo "$(date +'%F %T') \"$PROC_CMD\" restart." >> $WATCH_LOG
done > /dev/null 2>/dev/null & ;;
stop)
ppid=$(pgrep -f "$WATCH_NAME +start +[^ ]*\<$PROC_NAME\>$PROC_ARGS$")
[ -z "$ppid" ] && echo "No WATCH on \"$PROC_CMD\" !" && exit 1
pid=$(pgrep -lP $ppid|grep -v '^[0-9]\+ \+sleep$'|awk '{print $1}')
[ -z "$pid" ] && echo "No process: \"$PROC_CMD\"" && kill -9 $ppid && exit 0
kill -9 $ppid
echo "$(date +'%F %T') \"$PROC_CMD\" quit manually." >> $WATCH_LOG
StopProc "$pid" 0
exit 0 ;;
*)
echo -e "\tUsage:$0 start|stop cmd argv ...\n" ;;
esac