Skip to content

Instantly share code, notes, and snippets.

@Deltachaos
Created October 17, 2011 18:02
Show Gist options
  • Save Deltachaos/1293305 to your computer and use it in GitHub Desktop.
Save Deltachaos/1293305 to your computer and use it in GitHub Desktop.
sample start script template
#!/bin/bash
PNAME="tail"
PNAMEFULL="Tail"
PPATH="${HOME}"
PBIN="tail -f /var/log/dmesg.log"
PLOG="${PPATH}/${PNAME}.log"
PIDPATH="${PPATH}/${PNAME}.pid"
USESCREEN=0
SCREENNAME="${PNAME}-${$}"
check-running() {
if [ -f "${PIDPATH}" ]; then
return 0
else
return 255
fi;
}
case ${1} in
start)
check-running
if [ ${?} -eq 0 ]; then
echo "${PNAMEFULL} is still running!"
echo "May you need to delete '${PIDPATH}'?"
exit 2
fi
echo -n "Starting ${PNAMEFULL}... "
case ${USESCREEN} in
1)
screen -AmdS "${SCREENNAME}" "/bin/bash -c '${PBIN} 2>&1 | tee -a ${PLOG}'"
screen -list | grep "${SCREENNAME}" | sed -n -r 's/\w([0-9]+).*/\1/p' > "${PIDPATH}"
echo -e "\e[00;32mdone!\e[00m"
;;
0)
nohup /bin/bash -c "${PBIN}"' 2>&1 | tee -a '"${PLOG}" 2> /dev/null &
echo ${!} > "${PIDPATH}"
echo -e "\e[00;32mdone!\e[00m"
;;
esac
;;
stop)
check-running
if [ ${?} -gt 0 ]; then
echo "${PNAMEFULL} is not running!"
else
echo -n "Stopping ${PNAMEFULL}... "
PADPID=$(cat "${PIDPATH}")
if [ "${PADPID}" ]; then
case ${USESCREEN} in
1)
screen -S "${PADPID}" -X quit
if [ ${?} -gt 0 ]; then
echo -e "\e[00;31mfailed!\e[00m"
exit 255
fi
;;
0)
kill "${PADPID}"
if [ ${?} -gt 0 ]; then
echo -e "\e[00;31mfailed!\e[00m"
exit 255
fi
;;
esac
else
echo -e "\e[00;31mfailed!\e[00m"
exit 255
fi
rm "${PIDPATH}"
echo -e "\e[00;32mdone\e[00m"
fi
;;
restart)
${0} stop
sleep 1
${0} start
;;
status)
check-running
if [ ${?} -gt 0 ]; then
echo "${PNAMEFULL} is not running!"
else
echo "${PNAMEFULL} is running!"
fi
;;
statuscode)
check-running
echo ${?}
;;
*)
echo "Usage: "${0}" {start|stop|restart|status|statuscode}"
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment