1. #!/bin/bash
    2. APP_NAME=mm-wiki
    3. INSTALL_NAME=install
    4. ROOT_DIR=$(cd "$(dirname "$0")";pwd)
    5. INSTALL_DIR=$(cd "$(dirname "$0")/install";pwd)
    6. APP_FILE=${ROOT_DIR}/${APP_NAME}
    7. INSTALL_FILE=${INSTALL_DIR}/${INSTALL_NAME}
    8. PID_FILE=${ROOT_DIR}/logs/${APP_NAME}.pid
    9. LOG_FILE=${ROOT_DIR}/logs/${APP_NAME}.log
    10. function install() {
    11. chmod +x ${INSTALL_FILE}
    12. ${INSTALL_FILE}
    13. return 0
    14. }
    15. function check_pid() {
    16. if [[ -f ${PID_FILE} ]];then
    17. pid=`cat ${PID_FILE}`
    18. if [[ -n ${pid} ]]; then
    19. res=`ps -p ${pid}|grep -v "PID TTY" |wc -l`
    20. return `echo ${res}`
    21. fi
    22. fi
    23. return 0
    24. }
    25. function start() {
    26. check_pid
    27. run_res=$?
    28. if [[ ${run_res} -gt 0 ]];then
    29. echo -n "${APP_NAME} is running already, pid="
    30. cat ${PID_FILE}
    31. return 1
    32. fi
    33. chmod +x ${APP_FILE}
    34. nohup ${APP_FILE} &> ${LOG_FILE} &
    35. echo $! > ${PID_FILE}
    36. echo "${APP_NAME} start running, pid=$!"
    37. }
    38. function stop() {
    39. pid=`cat ${PID_FILE}`
    40. kill ${pid}
    41. echo "${APP_NAME} stop."
    42. }
    43. function restart() {
    44. pid=`cat ${PID_FILE}`
    45. stop
    46. start
    47. }
    48. function status() {
    49. check_pid
    50. run_res=$?
    51. if [[ ${run_res} -gt 0 ]];then
    52. echo "status: start"
    53. else
    54. echo "status: stop"
    55. fi
    56. }
    57. function help() {
    58. echo "$0 install|start|stop|restart|status|pid"
    59. }
    60. function pid() {
    61. cat ${PID_FILE}
    62. }
    63. if [[ "$1" == "" ]]; then
    64. help
    65. elif [[ "$1" == "install" ]];then
    66. install
    67. elif [[ "$1" == "stop" ]];then
    68. stop
    69. elif [[ "$1" == "start" ]];then
    70. start
    71. elif [[ "$1" == "restart" ]];then
    72. restart
    73. elif [[ "$1" == "status" ]];then
    74. status
    75. elif [[ "$1" == "pid" ]];then
    76. pid
    77. else
    78. help
    79. fi