sh small_control.sh all start
sh small_control.sh 8080 stop
sh small_control.sh 8081 restart
current_dir=$(cd `dirname $0`; pwd)
case $2 in
start)
if [ $1 == "all" ]; then
for dir in `ls -d */ | grep 80`
do
sub_shell=${current_dir}/${dir}small.sh
if [ -x "$sub_shell" ]; then
$sub_shell stop >/dev/null 2>&1
$sub_shell start
fi
done
else
${current_dir}/$1/small.sh start
fi
;;
stop)
if[ $1 == "all" ];then
for dir in `ls -d */ | grep 80`
do
sub_shell=${current_dir}/${dir}small.sh
if [ -x "$sub_shell" ];then
$sub_shell stop
fi
done
else
${current_dir}/$1/small.sh stop
;;
restart)
if[ $1 == "all" ];then
for dir in `ls -d */ | grep 80`
do
sub_shell=${current_dir}/${dir}small.sh
if [ -x "$sub_shell" ];then
$sub_shell restart
fi
done
else
${current_dir}/$1/small.sh restart
;;
*)
echo "Usage: small_control.sh {port|all} {start|stop}"
echo "example: small_control.sh all start"
echo "example: small_control.sh 8081 stop"
exit 1
;;
esac
current_dir=$(cd `dirname $0` ; pwd)
grand_parent_dir=$(cd ${current_dir}/..;pwd)
port_num=${current_dir##*/}
dest_out=${current_dir}/small${port_num}.out
jar_file=${grand_parent_dir}/small.jar
pid_file=${current_dir}/${port_num}.pid
command="java -jar -Xms1024M -Xmx4096M -Dserver.port=${port_num} ${jar_file}"
isRunning() {
ps -p "$1" &> /dev/null
}
start() {
if [[ -f "$pid_file" ]]; then
pid=$(cat "$pid_file")
isRunning "$pid" && { echo "Alreday running [$pid]"; return 0 }
fi
do_start "$@"
}
do_start(){
cd $current_dir
echo $command
$command > $dest_out 2>&1 &
pid=$!
echo "$pid" > "$pid_file"
[[ -z $pid ]] && { echo "Failed to start"; return 1; }
echo "Started {$pid}"
}
stop(){
[[ -f $pid_file ]] || { echo "Not running (Pidfile not found)"; return 0; }
pid=$(cat "$pid_file")
isRunning "$pid" || { echo "not running (process ${pid}). remove pid file."; rm -f "$pid_file"; return 0; }
do_stop "$pid" "$pid_file"
}
do_stop(){
kill "$1" &> /dev/null || { echo "unable to kill process $1"; return 1; }
for i in $(seq 1 30); do
isRunning "$1" || { echo "stopped [$1]"; rm -rf "$2"; return 0; }
[[ $i -eq 10 ]] && kill "$1" &> /dev/null
[[ $i -eq 20 ]] && kill -9 "$1" &> /dev/null
sleep 1
done
echo "unable to kill process $1";
return 1;
}
restart(){
stop && start
}
status(){
[[ -f "$pid_file" ]] || { echo "not running"; return 3; }
pid=$(cat "$pid_file")
isRunning $pid || { echo "Not running (Pidfile not found)"; return 1; }
echo "running {$pid}"
return 0
}
case "$1" in
start)
start "$@"; exit $? ;;
stop)
stop "$@"; exit $? ;;
restart)
restart "$@"; exit $? ;;
status)
status "$@"; exit $? ;;
*)
echo "usage $0 {start|stop|restart|status}"
echo 1;;
esac