go.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/bin/bash
  2. #这里可替换为你自己的执行程序,其他代码无需更改
  3. APP_NAME=Cold_Api6200
  4. #使用说明,用来提示输入参数
  5. usage() {
  6. echo "Usage: sh 脚本名.sh [start|stop|restart|status]"
  7. exit 1
  8. }
  9. #检查程序是否在运行
  10. is_exist() {
  11. pid=$(ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}')
  12. #如果不存在返回1,存在返回0
  13. if [ -z "${pid}" ]; then
  14. return 1
  15. else
  16. return 0
  17. fi
  18. }
  19. #启动方法
  20. start() {
  21. is_exist
  22. if [ $? -eq "0" ]; then
  23. echo "${APP_NAME} is already running. pid=${pid} ."
  24. else
  25. mkdir -p logs
  26. export GO111MODULE=off
  27. nohup ./${APP_NAME} >> ./logs/nohup.out 2>&1 &
  28. echo "${APP_NAME} start success"
  29. fi
  30. }
  31. #停止方法
  32. stop() {
  33. is_exist
  34. if [ $? -eq "0" ]; then
  35. kill -9 $pid
  36. else
  37. echo "${APP_NAME} is not running"
  38. fi
  39. }
  40. #输出运行状态
  41. status() {
  42. is_exist
  43. if [ $? -eq "0" ]; then
  44. echo "${APP_NAME} is running. Pid is ${pid}"
  45. else
  46. echo "${APP_NAME} is NOT running."
  47. fi
  48. }
  49. #重启
  50. restart() {
  51. stop
  52. start
  53. }
  54. #根据输入参数,选择执行对应方法,不输入则执行使用说明
  55. case "$1" in
  56. "start")
  57. start
  58. ;;
  59. "stop")
  60. stop
  61. ;;
  62. "status")
  63. status
  64. ;;
  65. "restart")
  66. restart
  67. ;;
  68. *)
  69. usage
  70. ;;
  71. esac