curlServer.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/bin/sh
  2. ROOT=$(cd `dirname $0`/../../; pwd)
  3. APP_NAME=CurlServer
  4. EXE_NAME=$ROOT/lib/$APP_NAME
  5. #PID 代表是PID文件
  6. PID=$ROOT/pid/$APP_NAME\.pid
  7. #log
  8. LOG=$ROOT/logs/$APP_NAME\.out
  9. ERROR_LOG=$ROOT/logs/$APP_NAME_error\.out
  10. usage() {
  11. echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
  12. exit 1
  13. }
  14. #检查程序是否在运行
  15. is_exist(){
  16. pid=`ps -ef|grep $EXE_NAME|grep -v grep|awk '{print $2}' `
  17. #如果不存在返回1,存在返回0
  18. if [ -z "${pid}" ]; then
  19. return 1
  20. else
  21. return 0
  22. fi
  23. }
  24. start(){
  25. is_exist
  26. if [ $? -eq "0" ]; then
  27. echo ">>> ${EXE_NAME} is already running PID=${pid} <<<"
  28. else
  29. nohup $EXE_NAME >> $LOG 2 > $ERROR_LOG &
  30. echo $! > $PID
  31. echo ">>> start $EXE_NAME successed PID=$! <<<"
  32. fi
  33. }
  34. #停止方法
  35. stop(){
  36. #is_exist
  37. pidf=$(cat $PID)
  38. #echo "$pidf"
  39. echo ">>> APP PID = $pidf begin kill $pidf <<<"
  40. kill $pidf
  41. rm -rf $PID
  42. sleep 2
  43. is_exist
  44. if [ $? -eq "0" ]; then
  45. echo ">>> api 2 PID = $pid begin kill -9 $pid <<<"
  46. kill -9 $pid
  47. sleep 2
  48. echo ">>> $EXE_NAME process stopped <<<"
  49. else
  50. echo ">>> ${EXE_NAME} is not running <<<"
  51. fi
  52. }
  53. #输出运行状态
  54. status(){
  55. is_exist
  56. if [ $? -eq "0" ]; then
  57. echo ">>> ${EXE_NAME} is running PID is ${pid} <<<"
  58. else
  59. echo ">>> ${EXE_NAME} is not running <<<"
  60. fi
  61. }
  62. #重启
  63. restart(){
  64. stop
  65. start
  66. }
  67. #根据输入参数,选择执行对应方法,不输入则执行使用说明
  68. case "$1" in
  69. "start")
  70. start
  71. ;;
  72. "stop")
  73. stop
  74. ;;
  75. "status")
  76. status
  77. ;;
  78. "restart")
  79. restart
  80. ;;
  81. *)
  82. usage
  83. ;;
  84. esac
  85. exit 0