linux 部署java程序,使其成为服务

 一个小项目,linux环境下部署,记录一下:

启动脚本comm_mq如下:

#! /bin/sh
#chkconfig:345 61 61
#description:test2Run description
#-------------------------------------------------------------------
# 定义变量
#-------------------------------------------------------------------
APP_NAME=xc_mq
GREP_KEY="Diname="${APP_NAME}

# -Xms512m 设置JVM堆的初始内存
# -Xmx1024m 设置JVM堆的最大内存
# -Dlog4j.properties #设置log4j日志文件参数,可给JAVA程序调用,调用格式是#System.getProperty("log4j.properties")
#APP_OPTS="-Xrs -Xms512m -Xmx1024m -Dlog4j.properties=../conf/#log4j.properties"

# 程序主类
APP_CLASS="com.tbyf.thread.LanuchServer"

APP_CLASSPATH=${CLASSPATH}

# 检查xc_mq进程是否已经在运行,如果在运行则返回1,否则返回0
is_exist(){
 # ps -ef : 查询所有进程
 # grep -w "${GREP_KEY}" : 从所有进程中查出名称为xc_mq的进程,-w为精确查找
 # grep -v "grep" : 排除名称为grep的进程
 # awk '{print $2}' : 输出第二个参数,也就是进程号
 pid=`ps -ef | grep -w "${GREP_KEY}" | grep -v "grep" | awk '{print $2}'`

 # 判断进程号是否为空
 if [ -z "${pid}" ]
  then return 1
 else 
  return 0
 fi
}


status(){
 is_exist 
 if [ $? -eq "0" ]
  then echo "${APP_NAME} is running. pid=${pid} ."
 else
  echo "${APP_NAME} is not running"
 fi
}


start(){
 is_exist 
 if [ $? -eq "0" ]
  then echo "${APP_NAME} is already running. pid=${pid} ."
  return 0
 else
  echo "try to start ${APP_NAME} ... "
  
  
# 调用nohup命令启动xc_mq
  # 1>&- : 表示关闭标准输出日志到nohup.out  
  # 2>${APP_LOG} : 表示输出日志到../logs/log.log
  # 最后的& : 表示退出帐户/关闭终端时程序不退出
  #              nohup $JAVA_HOME/bin/java -${GREP_KEY} ${APP_OPTS} -#classpath #${APP_CLASSPATH} ${APP_CLASS} 1>&- 2>${APP_LOG} &
 nohup /home/xj/java/jdk1.8.0_171/bin/java -${GREP_KEY}   -classpath ${APP_CLASSPATH} /home/xj/mq.jar ${APP_CLASS} 1>&- &
  # 程序的启动需要一定的时间,这里设置暂停时间(3秒),单位是秒
                sleep 3

                is_exist
                if [ $? -eq "0" ]
                then
                        echo "${APP_NAME} is running now. pid=${pid}."
                        return 0
                else
                        echo "failed to start ${APP_NAME}! see ${APP_LOG} for more details."
                        return 1
                fi
 fi
}

# 停止xc_mq进程
stop()
{
 is_exist

 if [ $? -eq 0 ]
  then  echo "try to stop ${APP_NAME} ..."

   # 调用kill命令杀掉进程
   /usr/bin/kill -9  ${pid}

   if [ $? -ne 0 ]
    then echo "failed to stop ${APP_NAME}!"
    return 1
   else
    echo "${APP_NAME} stopped."
    return 0
   fi
 else
  echo "${APP_NAME} is not running!"
  return 1
 fi
}


# 重启xc_mq进程
restart(){
 stop
 start
}


# 显示帮助信息
help()
{
echo "status                    show the status of ${APP_NAME} server."
echo "start                    start the ${APP_NAME} server."
echo "stop                      stop the ${APP_NAME} server."
echo "restart                  restart the ${APP_NAME} server."
}


# 主函数
main()
{
 case "$1" in 
 status)  status;;
 start)    start;;
 stop)    stop;;
 restart)  restart;;
 *)        echo "command param error ! see follow help "; help;;
 esac
}

main restart

给脚本赋予执行权限,chmod +x comm_mq

让脚本随着机器启动而启动,chkconfig --add comm_mq  //把服务或脚本加入到chkconfig 管理之中

ps:以下三行代码是必须的:

#! /bin/sh
#chkconfig:345 61 61
#description:test2Run description
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值