server.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/bash
  2. #
  3. # Description: server
  4. #
  5. # Source function library
  6. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  7. NAME=SERVER_NAME
  8. USER=SERVER_USER
  9. DAEMON=SERVER_DAEMON
  10. PROCESS=/var/run/$NAME.pid
  11. if [ ! -f "$DAEMON" ]; then
  12. echo "$DAEMON not found"
  13. exit
  14. fi
  15. # Start the server
  16. start (){
  17. # Check serve's status
  18. start-stop-daemon --status --pidfile=$PROCESS
  19. code=$?
  20. if [ code == 0 ]; then
  21. echo "$NAME is already running"
  22. exit 0
  23. fi
  24. start-stop-daemon --start --make-pidfile --background --pidfile=$PROCESS --user=$USER --exec=$DAEMON
  25. res = $?
  26. echo "$NAME start successful, pid(`cat $PROCESS`)"
  27. }
  28. # Stop the server
  29. stop (){
  30. # Check serve's status
  31. start-stop-daemon --status --pidfile=$PROCESS
  32. code=$?
  33. if [ code != 0 ]; then
  34. echo "$NAME is not running"
  35. exit 0
  36. fi
  37. start-stop-daemon --stop --pidfile=$PROCESS
  38. echo "$NAME stop successful"
  39. }
  40. # Return the status of the server
  41. status (){
  42. start-stop-daemon --status --pidfile=$PROCESS
  43. code=$?
  44. if [ code == 0 ]; then
  45. echo "$NAME is running, pid(`cat $PROCESS`)"
  46. else
  47. echo "$NAME is not running"
  48. fi
  49. }
  50. # Restart the server
  51. restart (){
  52. stop
  53. start
  54. }
  55. usage (){
  56. echo "Usage: start|stop|restart|status"
  57. }
  58. case $1 in
  59. start)
  60. start
  61. ;;
  62. stop)
  63. stop
  64. ;;
  65. restart)
  66. restart
  67. ;;
  68. status)
  69. status
  70. ;;
  71. *)
  72. usage
  73. exit 1
  74. ;;
  75. esac