Linux Create Service

1. create startup.sh and shutdown.sh for program
Example:
/opt/Ping/startup.sh
#!/bin/bash

if [ -f /var/run/PingServer.pid ]; then
        exit;
else
        export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/Ping/PingServerApp_lib

        cd /opt/Ping
        nohup java -jar /opt/Ping/PingServerApp.jar &
        echo $! > /var/run/PingServer.pid
fi

/opt/Ping/shutdown.sh
#!/bin/bash

PID=$(cat /var/run/PingServer.pid)

kill -9 ${PID}
rm -rf /var/run/PingServer.pid

2. Create file /etc/rc.d/init.d/pingserver
#!/bin/bash
#
# chkconfig: 35 66 34
# description:
#

. /etc/rc.d/init.d/functions

WORK_DIR=/opt/Ping

case "$1" in
  start)
        cd ${WORK_DIR};
        ./startup.sh;
        ;;
  stop)
        cd ${WORK_DIR};
        ./shutdown.sh;
        ;;
  restart)
        cd ${WORK_DIR};
        ./shutdown.sh;
        ./startup.sh
        ;;
  *)
        echo $"Usage: $0 {start|stop|restart}"
        exit 2
esac

exit $RETVAL

3. create the application as linux service 
chkconfig --add pingserver

4. make automatically start
chkconfig pingserver on

Comments

Popular posts from this blog

Nginx Proxy & Load Balance & LNMP

Snort+barnyard2+Snorby CentOS 6.5_64 Installation

ORACLE Error