启动Java程序为Linux服务

不想用别人封装的程序,比如wrapper service。用自己的脚本搞定。

1.创建一个start.sh脚本,里面运行java程序:

-UU-:----F1  master_rest    18% L25    (Shell-script[sh])------------------------------------------------------------------------------------------------------------------------
#!/bin/bash                                                                                                                                                                      

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
CARRIER_HOME=/home/dist/carrier
export JAVA_HOME=/usr/jdk1.6
export PATH="$JAVA_HOME/bin:$PATH"

java -server -cp $CARRIER_HOME/site/master/rest/dependency/ -jar $CARRIER_HOME/site/master/rest/master-1.0.jar &

2.在/etc/init.d/中创建一个service脚本,比如master_rest

内容如下:

# Short-Description: master                                                                                                                                                       
# Description: master rest service                                                                                                                                               
### END INIT INFO                                                                                                                                                                

. /lib/lsb/init-functions

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
CARRIER_HOME=/home/dist/carrier

case "$1" in
  start)
     log_begin_msg "Starting master rest server"
     /bin/bash $CARRIER_HOME/scripts/start.sh >/dev/null 2>&1
     log_end_msg 0
     ;;
  stop)
     PID=`ps -ef | grep 'master-1.0.jar' | grep -v grep | awk '{print $2}'`
     log_begin_msg "Stopping master rest server"
     if [ ! -z "$PID" ]; then
	kill -15 $PID
     fi
     log_end_msg 0
     ;;
  restart)
     $0 stop
     $0 start
     ;;
  *)
     log_success_msg "Usage: service master_rest {start|stop|restart}"
     exit 1
esac

exit 0


重点注意是 , 这行命令导致终端不会输出信息。
>/dev/null 2>&1


你可能感兴趣的:(启动Java程序为Linux服务)