Atualizei meu script para o código a seguir. Embora não seja capaz de criar pidfile, mas está funcionando como eu quero. Se alguém tiver um roteiro melhor, por favor poste sua resposta.
#!/bin/bash
# chkconfig: 2345 95 05
# description: MY Application
# Source function library.
. /etc/init.d/functions
RETVAL=0
prog="myserver"
PIDFILE=/var/run/$prog.pid
LOCKFILE=/var/lock/subsys/$prog
PATH=/sbin:/usr/sbin:/bin:/usr/bin
JAVA=/usr/bin/java
DESC="MY Application"
DAEMON=$JAVA
DAEMON_HOME="/home/ganesh/MyServer/"
JAR=$DAEMON_HOME/MyServer.jar
DAEMON_ARGS="-Xms512m -Xmx4112m -jar $JAR"
SCRIPTNAME=/etc/init.d/myserver
#the user that will run the script
USER=root
#echo "All value sets"
start() {
if [ -f $LOCKFILE ];
then
echo "$DESC is already running. Exiting."
exit 1
else
echo -n "Starting $prog:"
cd $DAEMON_HOME
daemon --user $USER --pidfile $PIDFILE $DAEMON $DAEMON_ARGS >/dev/null 2>&1 &
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && touch $LOCKFILE
fi
return $RETVAL
}
stop() {
echo -n "Shutting down "$prog:
kill -9 'ps -ef | grep "$JAR" | grep -v grep | awk '{ print $2 }''
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $LOCKFILE
return $RETVAL
}
check_status() {
#echo -n "Checking $prog status: "
status $prog
RETVAL=$?
return $RETVAL
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
check_status
;;
restart)
stop
start
;;
*)
echo "Usage: $prog {start|stop|status|restart}"
exit 1
;;
esac
exit $RETVAL
EDITAR:
Outra versão do script acima em que estou criando o arquivo pid e usando-o para obter o status e parar o aplicativo. Acho que o script a seguir é mais confiável do que o acima.
#!/bin/bash
# chkconfig: 2345 95 05
# description: MY Application
# Source function library.
. /etc/init.d/functions
RETVAL=0
prog="myserver"
PIDFILE=/var/run/$prog.pid
LOCKFILE=/var/lock/subsys/$prog
PATH=/sbin:/usr/sbin:/bin:/usr/bin
JAVA=/usr/bin/java
DESC="MY Application"
DAEMON=$JAVA
DAEMON_HOME="/home/ganesh/MyServer/"
JAR=$DAEMON_HOME/MyServer.jar
DAEMON_ARGS="-Xms512m -Xmx4112m -jar $JAR"
SCRIPTNAME=/etc/init.d/myserver
#the user that will run the script
USER=root
#echo "All value sets"
start() {
if [ -f $PIDFILE ]; then
PID='cat $PIDFILE'
if [ -z "'pgrep $PID'" ] && [ "$PID" != "'ps aux|grep -vE 'grep|runuser|bash'|grep -w "$JAR"|awk '{print $2}''" ]; then
printf "%s\n" "Process dead but pidfile exists"
else
printf "$prog is already running!\n"
fi
else
printf "%-50s" "Starting $prog ..."
cd $DAEMON_HOME
daemon --user $USER $DAEMON $DAEMON_ARGS >/dev/null 2>&1 &
sleep 5
PID='ps aux|grep -vE 'grep|runuser|bash'|grep -w "$JAR"|awk '{print $2}''
if [ -z "$PID" ]; then
printf "[ \e[31mFAIL3[0m ]\n"
else
echo $PID > $PIDFILE
printf "[ \e[32mOK3[0m ]\n"
fi
fi
}
stop() {
printf "%-50s" "Shutting down $prog:"
if [ -f $PIDFILE ]; then
PID='cat $PIDFILE'
kill -HUP $PID 2>/dev/null
printf "[ \e[32mOK3[0m ]\n"
rm -f $PIDFILE
else
printf "[ \e[31mFAIL3[0m ]\n"
fi
}
check_status() {
printf "%-50s" "Checking $prog ..."
if [ -f $PIDFILE ]; then
PID='cat $PIDFILE'
if [ -z "'pgrep $PID'" ] && [ "$PID" != "'ps aux|grep -vE 'grep|runuser|bash'|grep -w "$JAR"|awk '{print $2}''" ]; then
printf "%s\n" "Process dead but pidfile exists"
else
printf "[ \e[32mRUNNING3[0m ]\n"
fi
else
printf "[ \e[31mSTOPPED3[0m ]\n"
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
status)
check_status
;;
restart)
stop
start
;;
*)
echo "Usage: $prog {start|stop|status|restart}"
exit 1
;;
esac
exit 1