[转]Java Daemon Startup Script
How to start a Java program as a Linux daemon with an /etc/init.d
script.
See also:
- http://www.source-code.biz/snippets/java/7.htm
- JBoss Daemon Startup Script
- Waiting for a process to stop
Here is the CentOS/RedHat/Ubuntu version of the SUSE script:
#!/bin/bash
#
# chkconfig: 345 99 05
# description: Java deamon script
#
# A non-SUSE Linux start/stop script for Java daemons.
#
# Derived from -
# Home page: http://www.source-code.biz
# License: GNU/LGPL (http://www.gnu.org/licenses/lgpl.html)
# Copyright 2006 Christian d'Heureuse, Inventec Informatik AG, Switzerland.
#
# History:
# 2010-09-21 Josh Davis: Changed 'sudo' to 'su', fix some typos, removed unused variables
# 2009-03-04 Josh Davis: Ubuntu/Redhat version.
# 2006-06-27 Christian d'Heureuse: Script created.
# 2006-07-02 chdh: Minor improvements.
# 2006-07-10 chdh: Changes for SUSE 10.0. # Set this to your Java installation
JAVA_HOME=/usr/java/latest serviceNameLo="myservice" # service name with the first letter in lowercase
serviceName="MyService" # service name
serviceUser="appuser" # OS user name for the service
serviceGroup="appgroup" # OS group name for the service
applDir="/var/lib/$serviceNameLo" # home directory of the service application
serviceUserHome="/home/$serviceUser" # home directory of the service user
serviceLogFile="/var/log/$serviceNameLo.log" # log file for StdOut/StdErr
maxShutdownTime=15 # maximum number of seconds to wait for the daemon to terminate normally
pidFile="/var/run/$serviceNameLo.pid" # name of PID file (PID = process ID number)
javaCommand="java" # name of the Java launcher without the path
javaExe="$JAVA_HOME/bin/$javaCommand" # file name of the Java application launcher executable
javaArgs="-jar $applDir/myservice.jar" # arguments for Java launcher
javaCommandLine="$javaExe $javaArgs" # command line to start the Java service application
javaCommandLineKeyword="myservice.jar" # a keyword that occurs on the commandline, used to detect an already running service process and to distinguish it from others # Makes the file $1 writable by the group $serviceGroup.
function makeFileWritable {
local filename="$1"
touch $filename || return 1
chgrp $serviceGroup $filename || return 1
chmod g+w $filename || return 1
return 0; } # Returns 0 if the process with PID $1 is running.
function checkProcessIsRunning {
local pid="$1"
if [ -z "$pid" -o "$pid" == " " ]; then return 1; fi
if [ ! -e /proc/$pid ]; then return 1; fi
return 0; } # Returns 0 if the process with PID $1 is our Java service process.
function checkProcessIsOurService {
local pid="$1"
if [ "$(ps -p $pid --no-headers -o comm)" != "$javaCommand" ]; then return 1; fi
grep -q --binary -F "$javaCommandLineKeyword" /proc/$pid/cmdline
if [ $? -ne 0 ]; then return 1; fi
return 0; } # Returns 0 when the service is running and sets the variable $pid to the PID.
function getServicePID {
if [ ! -f $pidFile ]; then return 1; fi
pid="$(<$pidFile)"
checkProcessIsRunning $pid || return 1
checkProcessIsOurService $pid || return 1
return 0; } function startServiceProcess {
cd $applDir || return 1
rm -f $pidFile
makeFileWritable $pidFile || return 1
makeFileWritable $serviceLogFile || return 1
cmd="nohup $javaCommandLine >>$serviceLogFile 2>&1 & echo $! >$pidFile"
su -m $serviceUser -s $SHELL -c "$cmd" || return 1
sleep 0.1
pid="$(<$pidFile)"
if checkProcessIsRunning $pid; then :; else
echo -ne "n$serviceName start failed, see logfile."
return 1
fi
return 0; } function stopServiceProcess {
kill $pid || return 1
for ((i=0; i<maxShutdownTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo -e "n$serviceName did not terminate within $maxShutdownTime seconds, sending SIGKILL..."
kill -s KILL $pid || return 1
local killWaitTime=15
for ((i=0; i<killWaitTime*10; i++)); do
checkProcessIsRunning $pid
if [ $? -ne 0 ]; then
rm -f $pidFile
return 0
fi
sleep 0.1
done
echo "Error: $serviceName could not be stopped within $maxShutdownTime+$killWaitTime seconds!"
return 1; } function startService {
getServicePID
if [ $? -eq 0 ]; then echo -n "$serviceName is already running"; RETVAL=0; return 0; fi
echo -n "Starting $serviceName "
startServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "started PID=$pid"
RETVAL=0
return 0; } function stopService {
getServicePID
if [ $? -ne 0 ]; then echo -n "$serviceName is not running"; RETVAL=0; echo ""; return 0; fi
echo -n "Stopping $serviceName "
stopServiceProcess
if [ $? -ne 0 ]; then RETVAL=1; echo "failed"; return 1; fi
echo "stopped PID=$pid"
RETVAL=0
return 0; } function checkServiceStatus {
echo -n "Checking for $serviceName: "
if getServicePID; then
echo "running PID=$pid"
RETVAL=0
else
echo "stopped"
RETVAL=3
fi
return 0; } function main {
RETVAL=0
case "$1" in
start) # starts the Java program as a Linux service
startService
;;
stop) # stops the Java program service
stopService
;;
restart) # stops and restarts the service
stopService && startService
;;
status) # displays the service status
checkServiceStatus
;;
*)
echo "Usage: $0 {start|stop|restart|status}"
exit 1
;;
esac
exit $RETVAL
} main $1
[转]Java Daemon Startup Script的更多相关文章
- 升级gitk后,Error in startup script: unknown color name "lime"
$ gitkError in startup script: unknown color name "greeen" (processing "-fore" o ...
- Java过滤任意(script,html,style)标签符,返回纯文本--封装类
import java.util.regex.Pattern; /** * 过滤标签字符串,返回纯文本 * */ public class ChangePlainText { ...
- java线程基础知识----java daemon线程
java线程是一个运用很广泛的重点知识,我们很有必要了解java的daemon线程. 1.首先我们必须清楚的认识到java的线程分为两类: 用户线程和daemon线程 A. 用户线程: 用户线程可以简 ...
- JAVA DAEMON线程的理解
java线程分两种:用户线程和daemon线程.daemon线程或进程就是守护线程或者进程,但是java中所说的daemon线程和linux中的daemon是有一点区别的. linux中的daemon ...
- Java Daemon 守护线程
Java中可以通过Thread或ThreadGroup的setDaemon方法将线程设置为守护线程 当所有非守护线程退出后 守护线程将被杀死不在运行 .Net中可以通过设置IsBackground属性 ...
- Java daemon thread 守护线程
守护线程与普通线程写法上基本么啥区别,在启动线程前, 调用线程对象的方法setDaemon(true),则可以将其设置为守护线程. 守护线程使用的情况较少,但并非无用,举例来说,JVM的垃圾回收.内存 ...
- Java菜鸟学习 Script 脚本语言(简介)
script 可以写在head里 也可以写在body里 还可以写在 /html后面 script 也是成对出现的 <script></script> 他有三种常见的对话框 1 ...
- FreeBSD 10 发布
发行注记:http://www.freebsd.org/releases/10.0R/relnotes.html 下文翻译中... 主要有安全问题修复.新的驱动与硬件支持.新的命名/选项.主要bug修 ...
- JAVA MemCache 史无前例的详细讲解【转】
非原创转自:http://nhy520.iteye.com/blog/1775893 这篇文章是我看到的介绍的比较详细的,入门级别算是足足够了 Memcach什么是Memcache Memcache集 ...
随机推荐
- 一道在知乎很火的 Java 题——如何输出 ab【转】
这是一个源自知乎的话题,原贴链接:一道百度的面试题,有大神会嘛? 虽然我不是大神,但我也点进去看了一下,思考了一会之后有了一些思路,然后去看其它人的答案的时候果然全都已经被各路大神们先想到并贴出来了, ...
- @ManyToMany 两个表多对多关联
两个表属于多对多关系 如 Teacher <=> Student 表teacher 主键 id 表student 主键id 中间关联表 teacher_student 两个字段 t_id ...
- 常用布局,div竖直居中
常用两列布局,多列布局和div竖直居中 body { margin:; padding:; } .w200 { width: 200px; } .mar-left200 { margin-left: ...
- Canvas transform浅析
没有前奏,直接进入主题 transform调用方法: ctx.transform(a,b,c,d,e,f);如下 var ctx = document.getElementById("myC ...
- GridView点击行,选中模版列中CheckBox
<asp:TemplateField ItemStyle-Width="40px" HeaderText="选择" ItemStyle-Horizonta ...
- C# 实现文件夹的复制以及删除
代码来源:http://blog.163.com/u_tommy_520/blog/static/20406104420147493933662/ http://www.cnblogs.com/lov ...
- Android系统下的动态Dex加载与app速度优化
1 问题 在Android系统中,一个App的所有代码都在一个Dex文件里面.Dex是一个类似Jar的存储了多有Java编译字节码的归档文件.因为Android系统使用Dalvik虚拟机,所以需要把 ...
- 本地代码上传 -> Github
首先在控制台cd到你的本地项目,这里以teat为例 1.执行命令: git init 2.将项目文件添加到仓库中: git add . (可以是指定文件,将“.”转换为指定文件) 3.接下来com ...
- 通过Unity依赖注入
前言 Unity容器的思想起始于我在为Web Client Sofitware Factory项目工作的时候,微软的patterns&practices团队已经使用依赖注入的概念好几年了在那时 ...
- 【COGS495】窗口
[问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: Window position Min value Max val ...