Instantly share code, notes, and snippets.

Embed
What would you like to do?
java Daemon control script
#!/usr/bin/env bash
###########################################
# Linux/Unix Daemon control script #
# @author : Bohyung kim<dsdgun@gmail.com> #
# @since : 2013.06.06 #
###########################################
# 기본경로는 현재 위치지만 따로 설정할수도 있다.
__workdir=`pwd`
# application 이름
__appname=WenestIndexServer
# Background 로 띄울 Process commandline
__command="java -XX:-UseSplitVerifier -XX:-HeapDumpOnOutOfMemoryError -jar your_application.jar"
# log 파일 경로
__stdout_log=stdout.log
__stderr_log=stderr.log
# pid file 경로
__pidfile=${__workdir}/${__appname}.pid
function __status() {
if [ -e $__pidfile ]; then
local _pid=`cat $__pidfile`
_flag=`ps -f $_pid | wc -l`
if [ $_flag != 2 ]; then
rm -f ${__pidfile}
__log "Process is not running"
exit -1
fi
__log "Process is already running" $_pid
exit -1
else
__log "Process is not running"
fi
}
function __start_daemon(){
nohup $__command 1> $__stdout_log 2> $__stderr_log &
local _pid=$!
echo $_pid > $__pidfile
__log "start daemon pid [$_pid]"
}
function __stop_daemon() {
if [ ! -e $__pidfile ]; then
__log "Process does not running"
exit -1
fi
local _pid=`cat $__pidfile`
local _aliveflag=`ps -f $_pid | wc -l`
if [ $_aliveflag != 2 ];then
__log "Process does not running"
rm -f $__pidfile
else
__log "stopping daemon"
kill -15 ${_pid}
# wait for process kill
while [ $_aliveflag == 2 ]; do
printf .
sleep 0.5
_aliveflag=`ps -f $_pid | wc -l`
done
printf "\n"
__log "daemon stopped."
rm -f $__pidfile
fi
}
function __restart_daemon() {
[ -e $__pidfile ] && __stop_daemon
__start_daemon
}
function __log(){
echo -e "\033[00;33m$__appname - \033[00;37m$*"
}
case "$1" in
start) __start_daemon ;;
stop) __stop_daemon ;;
restart) __restart_daemon ;;
status) __status ;;
*) __log "Usage : $0 <start|stop|restart|status>" ;;
esac
@dsdstudio

This comment has been minimized.

Owner

dsdstudio commented Jun 7, 2013

daemon 종료시 pid file 지워지지않는 문제 수정

@dsdstudio

This comment has been minimized.

Owner

dsdstudio commented Jun 7, 2013

Centos 에서 로깅시 color code 그대로 나오는문제 해결

@dsdstudio

This comment has been minimized.

Owner

dsdstudio commented Jun 7, 2013

pid파일 이름 application 이름을 따르도록 변경( __appname.pid)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment