注册程序为LINUX系统服务并设置成自启动的方法

来源:岁月联盟 编辑:exp 时间:2012-04-12

注册程序为LINUX系统服务并设置成自启动的方法 这里以red hat linux 为例, 设置某个JAVA程序为系统服务, 通过service命令进行管理, 并跟随系统自行启动. 1.  先将自己的程序写入SH脚本, 便于管理.如: java -server -cp .:./lib/* com.test.Startup  2. 进入/etc/init.d目录, 新建文件, 以自己的程序命名, 如: erp内容如下:[plain]#!/bin/sh  # chkconfig: 35 99 1  #  www.2cto.com  # description: ERP service  #      ERP_HOME=/data/falconprobuf       RETVAL=0    ERP_PORT=8099       # start and stop functions    start() {        pids=`netstat -lnp | grep $ERP_PORT |awk '{print $7 }'`      pids=${pids%/*}      if [ -n "$pids" ]; then                 echo  "ERP SERVICE ALREADY START "      else       echo "START ERP SERVICE "              cd $ERP_HOME              ./startup.sh &              echo        fi             }         stop() {               pids=`netstat -lnp | grep $ERP_PORT |awk '{print $7 }'`       pids=${pids%/*}   www.2cto.com      if [ -n "$pids" ]; then      echo  "STOP ERP SERVICE "              kill -9 $pids      echo  "STOP ERP SUCCESS "       else      echo  "ERP SERVICE ALREADY STOP "        fi          }         # See how we were called.    www.2cto.com  case "$1" in      start)            start            ;;      stop)            stop            ;;      restart)            stop            sleep 2                    start            ;;      *)            echo "Usage: $0 {start|stop|restart}"            exit 1    esac   ① 注意第二行: # chkconfig: 35 99 1这个需要加上, 因为后面要设置成自启动方式.② ./startup.sh &  www.2cto.com  加上&, 挂入后台运行, 不会影响当前的连接会话.这里没有使用全路径,  因为执行脚本里面使用了相对路径, 会找不到相应的JAR包,
解决办法是先跳入目录, cd $ERP_HOME, 再执行脚本.
③ERP_PORT=8099设置端口, 通过端口过滤的方式来结束程序, 会更为准确, 不会干扰其他的程序运行. 3. 确认服务文件的执行权限,  执行chmod +x erp  不需重启, 这时可通过SERVCE start|stop|restart 方式进行服务管理.最后执行chkconfig --add erp 列入运行服务,   完成配置.   摘自 hxx688的专栏