Created
March 24, 2021 09:35
-
-
Save azhai/403c6aef5ff28a0a65458f98064825d2 to your computer and use it in GitHub Desktop.
保持脚本一直运行中
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# * * * * * /home/ryan/bin/script_keeper.sh | |
APP_BIN="/usr/bin/python" | |
APP_FILE="/home/ryan/tools/remove_vod_files.py" | |
PROGS=( | |
"asc" | |
"desc" | |
) | |
PROC_ID="" | |
PROC_ID_LIST="" | |
get_concat_name() { | |
echo "$*" | tr ' ' '-' | |
} | |
grep_process_pids() { | |
local params="$*" | |
local app_file="$APP_FILE $params" | |
PROC_ID_LIST=`ps -efww | grep "$app_file" | grep -v grep | cut -c 9-15 | xargs` | |
if [ -z "$PROC_ID_LIST" ]; then | |
echo "Process $params is not running" | |
fi | |
} | |
start_server() { | |
local params="$*" | |
local name=$(get_concat_name $params) | |
local app_file="$APP_FILE $params" | |
$APP_BIN $app_file > /dev/null 2>&1 & | |
echo "$params starting" | |
} | |
stop_server() { | |
local params="$*" | |
grep_process_pids $params | |
if [ ! x"$PROC_ID_LIST" = x ]; then | |
kill -9 $PROC_ID_LIST | |
fi | |
echo "$params closed" | |
} | |
keep_server_running() { | |
local params="$*" | |
grep_process_pids $params | |
if [ x"$PROC_ID_LIST" = x ]; then | |
start_server $params | |
fi | |
} | |
case "$1" in | |
start) | |
for name in "${PROGS[@]}" ; do | |
start_server $name | |
done | |
;; | |
stop) | |
for name in "${PROGS[@]}" ; do | |
stop_server $name | |
done | |
;; | |
restart | reload) | |
for name in "${PROGS[@]}" ; do | |
stop_server $name | |
start_server $name | |
done | |
;; | |
forever) | |
for i in {1..12}; do | |
for name in "${PROGS[@]}" ; do | |
keep_server_running $name | |
done | |
sleep 5 | |
done | |
;; | |
*) | |
for name in "${PROGS[@]}" ; do | |
keep_server_running $name | |
done | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment