Created
February 28, 2017 00:32
-
-
Save abhijitmehta/e785f2f518ab90654603bb0270b33efc to your computer and use it in GitHub Desktop.
start , stop , monitor "python app.py"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# !/bin/ksh | |
Init() | |
{ | |
#(1) Set a work directory | |
WORK_DIR="$( pwd )/work" | |
if [ ! -d ${WORK_DIR} ] | |
then | |
mkdir $WORK_DIR | |
if [ $? -ne 0 ] | |
then | |
WORK_DIR=$( pwd ) | |
fi | |
fi | |
#(2) All the directories where logs are located | |
LOG_DIR="${WORK_DIR}/log" | |
if [ ! -d ${LOG_DIR} ] | |
then | |
mkdir $LOG_DIR | |
if [ $? -ne 0 ] | |
then | |
LOG_DIR=$( pwd ) | |
fi | |
fi | |
#(3) Set Flag Names for START/STOP | |
procName="python_app_py" | |
START_FLAG=${WORK_DIR}/${procName}.started | |
STOP_FLAG=${WORK_DIR}/${procName}.stopped | |
} | |
startIt() | |
{ | |
echo -e "\n \033[36m .....Attempting Start ${procName} ! \033[0m \n" | |
if [ -e ${STOP_FLAG} ] | |
then | |
rm -f ${STOP_FLAG} | |
fi | |
logFile=${LOG_DIR}/$$.out.log | |
errorFile=${LOG_DIR}/$$.error.log | |
if [ -e ${START_FLAG} ] | |
then | |
echo " .................Seems Process is already Started (flag exists). Checking for daemon ! " | |
for pid in $( ps -efl | grep -v grep | grep "python app.py" | awk -F" " '{print $2}' ) | |
do | |
echo "${procName} is running with pid ${pid}" | |
done | |
else | |
( python app.py 1>>${logFile} 2>>${errorFile}) & | |
if [ "$!" != "" ] | |
then | |
echo -e "\n \033[36m ..... Process Initiated with pid $! ! \033[0m \n" | |
touch $START_FLAG | |
else | |
echo -e "\n \033[36m .....Trouble Starting your Process ! \033[0m \n" | |
fi | |
fi | |
} | |
stopIt() | |
{ | |
echo -e "\n \033[36m .....Setting flags to stop ${procName} ! \033[0m \n" | |
touch ${STOP_FLAG} | |
rm -f ${START_FLAG} | |
for pid in $( ps -efl | grep -v grep | grep "python app.py" | awk -F" " '{print $2}' ) | |
do | |
echo -e "\n \033[36m .....Stopping ${pid} now \033[0m " | |
kill -9 ${pid} | |
done | |
echo -e "\n \033[36m ..... Stopped Process \033[0m \n" | |
} | |
monitorIt() | |
{ | |
if [ -e ${STOP_FLAG} ] | |
then | |
echo " Process stopped by User , not starting it . If you want this to be started , remove ${STOP_FLAG} " | |
else | |
startIt | |
fi | |
} | |
main() | |
{ | |
Init $* | |
if [[ "$1" != "start" && "$1" != "stop" && "$1" != "monitor" ]] | |
then | |
echo -e "\n \033[36m ..... INVALID OPTION \033[0m " | |
echo -e " \033[36m ..... USAGE :: \033[0m " | |
echo -e " \033[36m ..... ./startStopMonitor.sh start \033[0m " | |
echo -e " \033[36m ..... OR :: \033[0m " | |
echo -e " \033[36m ..... ./startStopMonitor.sh stop \033[0m " | |
echo -e " \033[36m ..... OR :: \033[0m " | |
echo -e " \033[36m ..... ./startStopMonitor.sh monitor \033[0m " | |
echo -e "\n \n" | |
else | |
${1}It | |
fi | |
} | |
main $* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment