server.sh 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. SERVERPATH=SERVER_PATH
  12. if [ ! -f "$DAEMON" ]; then
  13. echo "$DAEMON not found"
  14. exit
  15. fi
  16. # Start the server
  17. start (){
  18. # Check serve's status
  19. start-stop-daemon --status --pidfile=$PROCESS
  20. if [ $? == 0 ]; then
  21. echo "$NAME is already running"
  22. exit
  23. fi
  24. start-stop-daemon --start --make-pidfile --background --pidfile=$PROCESS --chuid=$USER --chdir=$SERVERPATH --exec=$DAEMON
  25. test $? -ne 0 || echo "$NAME start successful, pid(`cat $PROCESS`)"
  26. }
  27. # Stop the server
  28. stop (){
  29. # Check serve's status
  30. start-stop-daemon --status --pidfile=$PROCESS
  31. if [ $? != 0 ]; then
  32. echo "$NAME is not running"
  33. exit
  34. fi
  35. start-stop-daemon --stop --pidfile=$PROCESS
  36. test $? -ne 0 || echo "$NAME stop successful"
  37. }
  38. # Return the status of the server
  39. status (){
  40. start-stop-daemon --status --pidfile=$PROCESS
  41. if [ $? == 0 ]; then
  42. echo "$NAME is running, pid(`cat $PROCESS`)"
  43. else
  44. echo "$NAME is not running"
  45. fi
  46. }
  47. # Restart the server
  48. restart (){
  49. stop
  50. start
  51. }
  52. usage (){
  53. echo "Usage: start|stop|restart|status"
  54. }
  55. case $1 in
  56. start)
  57. start
  58. ;;
  59. stop)
  60. stop
  61. ;;
  62. restart)
  63. restart
  64. ;;
  65. status)
  66. status
  67. ;;
  68. *)
  69. usage
  70. exit 1
  71. ;;
  72. esac