Skip to content

Instantly share code, notes, and snippets.

@dsdstudio
Last active December 18, 2015 03:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsdstudio/5720403 to your computer and use it in GitHub Desktop.
Save dsdstudio/5720403 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@dsdstudio
Copy link
Author

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

@dsdstudio
Copy link
Author

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

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