博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Start Java program as Linux daemon
阅读量:5993 次
发布时间:2019-06-20

本文共 4141 字,大约阅读时间需要 13 分钟。

   
 
     
 
Author: Category: Posted on: 16-02-2011
 
 

Use this script to run Java programs as daemons (services) on Linux Centos (or other Red Hat releases). This script originates from a script use to do the same thing in Linux Suse and was adapted for Centos by . Save the code below in a sh file (Ex.: daemon.sh), set the variables at the beginning of the script. To start/stop the daemon using daemon.sh, just use:

./daemon.sh start

./daemon.sh stop

./daemon.sh status

 

#!/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 installationJAVA_HOME=/usr/java/latestserviceNameLo="myservice"                                  # service name with the first letter in lowercaseserviceName="MyService"                                    # service nameserviceUser="appuser"                                      # OS user name for the serviceserviceGroup="appgroup"                                    # OS group name for the serviceapplDir="/var/lib/$serviceNameLo"                          # home directory of the service applicationserviceUserHome="/home/$serviceUser"                       # home directory of the service userserviceLogFile="/var/log/$serviceNameLo.log"               # log file for StdOut/StdErrmaxShutdownTime=15                                         # maximum number of seconds to wait for the daemon to terminate normallypidFile="/var/run/$serviceNameLo.pid"                      # name of PID file (PID = process ID number)javaCommand="java"                                         # name of the Java launcher without the pathjavaExe="$JAVA_HOME/bin/$javaCommand"                      # file name of the Java application launcher executablejavaArgs="-jar $applDir/myservice.jar"                     # arguments for Java launcherjavaCommandLine="$javaExe $javaArgs"                       # command line to start the Java service applicationjavaCommandLineKeyword="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

转载地址:http://ervlx.baihongyu.com/

你可能感兴趣的文章
HDU 1017 A Mathematical Curiosity【看懂题意+穷举法】
查看>>
Go --- GC优化经验
查看>>
【树莓派】【转】树莓派3装Android 6.0,支持Wi-Fi和蓝牙
查看>>
报错:Binary XML file line #7: Error inflating class android.support.v7.widget.RecyclerView
查看>>
LeetCode OJ 之 Valid Anagram
查看>>
Http Hijacker
查看>>
7.11
查看>>
MySQL和B树的那些事
查看>>
Atitti 互联网时代三大竞争战略 ——平台化战略 锚”战略、价值领先战略
查看>>
Mysql 常用函数汇总
查看>>
Dojo入门篇
查看>>
LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
查看>>
HDU 5353 Average
查看>>
神经网络入门游戏推荐BugBrain
查看>>
【webpack】webpack-dev-server生猛上手——让我们来搭一个webpack的微服务器吧!
查看>>
王立平--TF卡
查看>>
HTML5中x-webkit-speech语音输入功能
查看>>
按键驱动程序(异步通知)
查看>>
Linux 文件系统初步
查看>>
hdu 4521 小明系列问题——小明序列(线段树+DP或扩展成经典的LIS)
查看>>