Wednesday, December 10, 2014

Shell - Start and Stop a application on Linux

#!/bin/sh
#
# This is the shell script to release for something.
#
###############
# -eq -> ==
# -ne -> !=
# -gt -> >
# -lt -> <
# -ge -> >=
# -le -> <=
################
APP_NAME=test
# Check the process count
APP_CNT=`/bin/ps -ef | /bin/grep "${APP_NAME}" | /bin/grep -v "grep\|init.d" -c`
if [ ${APP_CNT} -gt 0 ]; then
echo "${APP_NAME} is already runing..."
exit 1
fi
echo ${APP_CNT}
case "$1" in
start)
printf "%-50s" "Starting $NAME..."
/etc/init.d/${APP_NAME} start
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo "successful start"
else
echo "failure starting"
fi
;;
stop)
printf "%-50s" "Starting $NAME..."
/etc/init.d/${APP_NAME} stop
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo "successful stop"
else
echo "failure stopping"
fi
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
view raw RestartApp hosted with ❤ by GitHub