[转]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集 ...
随机推荐
- golang中channel的超时处理
并发中超时处理是必不可少的,golang没有提供直接的超时处理机制,但可以利用select机制来解决超时问题. func timeoutFunc() { //首先,实现并执行一个匿名的超时等待函数 t ...
- ubuntu下安装tomcat和配置mysql
1.到官网 http://jakarta.apache.org/tomcat-7.0.2.tar.gz 安装文件,JDK假设已经安装完毕,接下来直接安装tomcat-7.0.2.tar.gz # ls ...
- RHEL7下PXE+NFS+Kickstart无人值守安装操作系统
RHEL7下PXE+NFS+Kickstart无人值守安装操作系统 1.配置yum源 vim /etc/yum.repos.d/development.repo [development] name= ...
- bitbucket/github同一站点上多个git代码仓库的ssh-key配置
由于项目开发需要,可能多个项目都放在bitbucket或者github上面,每个项目都有独立的sshkey,这就会造成push时的系统默认取~/.ssh/id_rsa的问题. 最简单的解决方法是这样: ...
- 二分图最大匹配(匈牙利算法Dfs模板)
#include<iostream> #include<cstdio> #include<cstring> #define maxn 2020 using name ...
- HDU5339
题意:给你数a和数组b,然后用a模b中的数,求至少模多少个才能使a==0 思路:直接模拟吧,首先排序,因为模最大的符合(比如2,3,6)然后遍历b,去模其他的所有数,直到为0,标记退出,否则继续遍历b ...
- Spring框架
Spring框架的根本使命是:简化JAVA开发,为了简化开发,有以下四个策略 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面和惯性进行声明式编程: 通过切面和模板 ...
- MemCache缓存和C#自带的Cache缓存
1.MemCache: //初始化 static SockIOPool _pool; // 创建Memcached private static MemcachedClient Create(stri ...
- (转)PHP的ereg()与eregi()的不同及相同点。对比
ereg() 字符串比对解析. 语法: int ereg(string pattern, string string, array [regs]); 返回值: 整数/数组 函数种类: 资料处理 内容说 ...
- Android 的 Relative Layout 常量
android:layout_above 将该控件的底部置于给定ID的控件之上 --Rule that aligns a child's bottom edge ...