Skip to content

Instantly share code, notes, and snippets.

@vietor
Last active December 25, 2015 20:29
Show Gist options
  • Save vietor/7035839 to your computer and use it in GitHub Desktop.
Save vietor/7035839 to your computer and use it in GitHub Desktop.
#!/bin/bash
declare -a array
array[0]="/usr/local/php/bin/php /wwwroot/kbl/tools/ClearGameLog.php"
array[1]="/usr/local/php/bin/php /wwwroot/kbl/tools/ProcessPvpRating.php"
let array_max=${#array[@]}-1
function is_index_exists {
if [ -z "$1" ]; then
return 1
fi
if [[ "$1" = "${1//[^0-9]/}" ]]; then
if [[ $1 -le $array_max ]]; then
return 0
fi
fi
return 1
}
function is_cmd_norun {
pid=`ps aux|grep "$1"|grep -v grep|awk '{print $2}'`
if [[ "$pid" = "" ]]; then
return 0
else
return 1
fi
}
function print_index_warning {
echo "Error: bad index [$1] in [0 ~ $array_max]"
}
function cmd_list {
echo "Script List:"
for((i=0;i<${#array[@]};i++))
do
cmd=${array[$i]}
if is_cmd_norun "$cmd"; then
status="---"
else
status="RUN"
fi
echo " [$i] -{$status}- [$cmd]"
done
}
function cmd_stop {
echo "Script Stop:"
cmd=${array[@]:$1:1}
echo " [$cmd]"
if is_cmd_norun "$cmd"; then
status="ALREADY STOPED"
else
ps aux|grep "$cmd"|grep -v grep|awk '{print $2}'|xargs kill -s 9
if is_cmd_norun "$cmd"; then
status="SUCCESS"
else
status="FAIL"
fi
fi
echo " $status"
}
function cmd_start {
echo "Script Start:"
cmd=${array[@]:$1:1}
echo " [$cmd]"
if is_cmd_norun "$cmd"; then
$cmd 1>/dev/null 2>/dev/null&
if is_cmd_norun "$cmd"; then
status="FAIL"
else
status="SUCCESS"
fi
else
status="ALREADY STARTED"
fi
echo " $status"
}
function cmd_restart {
cmd_stop $1
cmd_start $1
}
function cmd_stop_all {
for((i=0;i<${#array[@]};i++))
do
cmd_stop $i
done
}
function cmd_start_all {
for((i=0;i<${#array[@]};i++))
do
cmd_start $i
done
}
function cmd_restart_all {
for((i=0;i<${#array[@]};i++))
do
cmd_restart $i
done
}
case "$1" in
list)
cmd_list
;;
stop)
if [[ "$2" = "all" ]]; then
cmd_stop_all
elif is_index_exists $2; then
cmd_stop $2
else
print_index_warning $2
fi
;;
start)
if [[ "$2" = "all" ]]; then
cmd_start_all
elif is_index_exists $2; then
cmd_start $2
else
print_index_warning $2
fi
;;
restart)
if [[ "$2" = "all" ]]; then
cmd_restart_all
elif is_index_exists $2; then
cmd_restart $2
else
print_index_warning $2
fi
;;
*)
echo "Usage: $0 {list|start <index>|stop <index>|restart <index>}"
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment