server.sh 1.3 KB

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