Skip to content

Instantly share code, notes, and snippets.

@fushihara
Last active August 29, 2015 14:12
Show Gist options
  • Save fushihara/7986884443c47e151987 to your computer and use it in GitHub Desktop.
Save fushihara/7986884443c47e151987 to your computer and use it in GitHub Desktop.
Javaに特化した起動スクリプト http://fushi.x0.com/blog/archives/1677
#!/bin/sh
#
# chkconfig: 345 70 30
# description: Fushihara java run test
java=/usr/java/default
prog=javaTest
currentDirectory=/home/fushihara/javaTest/
classPath=bin:src
runClass=org.fushihara.javastdinouttest.Test
javaoptions=
outStd=/var/log/stdOutText.txt
outErr=/var/log/stdErrText.txt
if [ -z ${outStd} ]; then
outStd=/dev/null
fi
if [ -z ${outErr} ]; then
outErr=/dev/null
fi
cmdline="${java}/bin/java -Dfile.encoding=UTF-8 -Xbootclasspath/a:${classPath} ${runClass} ${javaoptions}"
RETVAL=0
#echo -e "\e[42;97m[OK]\e[0m"
#echo -e "\e[41;97m[NG]\e[0m"
start() {
PROC=`getpid`
if [ ${PROC} -eq 0 ]; then
cd ${currentDirectory}
echo -n $"${prog}を起動中…"
`${cmdline} >> ${outStd} 2>> ${outErr} ` < /dev/null &
PROC2=`getpid`
if [ ${PROC2} -ne 0 ]; then
echo -e "\e[42;97m[成功:${PROC2}]\e[0m"
else
echo -e "\e[41;97m[失敗]\e[0m"
fi
else
echo -e $"${prog}(PID=${PROC})は\e[41;97m起動中\e[0mです"
fi
}
stop() {
PROC=`getpid`
if [ ${PROC} -ne 0 ]; then
kill ${PROC}
echo -e "${prog}を\e[42;97m終了\e[0mしました"
else
echo -e "${prog}は実行されて\e[41;97mいません\e[0mでした"
fi
}
status(){
PROC=`getpid`
if [ ${PROC} -ne 0 ]; then
echo -e "${prog}(PID=${PROC})は\e[42;97m実行中\e[0mです"
else
echo $"${prog}は\e[41;97m停止\e[0mしています"
fi
}
getpid(){
RES=`ps ax | grep ${runClass} | grep -v ' grep ' | awk '{print$1}'`
if [ -z ${RES} ]; then
RES=0
fi
echo ${RES}
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo $"Usage: ${prog} {start|stop|status|restart}: "
RETVAL=2
esac
exit $RETVAL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment