glances 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:
  4. # Required-Start: $remote_fs $syslog
  5. # Required-Stop: $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: Start daemon at boot time
  9. # Description: Enable service provided by daemon.
  10. ### END INIT INFO
  11. ###
  12. # Origin code: Felix H. Dahlke
  13. # URL: https://github.com/fhd/init-script-template
  14. # Licence: MIT; https://github.com/fhd/init-script-template/blob/master/LICENSE
  15. #
  16. # Modified by me: Stephane HUC <devs@stephane-huc.net>
  17. ###
  18. ###
  19. # To place into /etc/init.d; chown root; chmod 0755
  20. ###
  21. dir="/usr/local/bin/" # directory where script glance is putted
  22. cmd="glances" # don't modify, or at yours risks!
  23. options="" # place here, options to run the service; see documentations!
  24. user="root" # change id user, if necessary...
  25. name=`basename $0`
  26. pid_file="/var/run/$name.pid"
  27. stdout_log="/var/log/$name.log"
  28. stderr_log="/var/log/$name.err"
  29. get_pid() {
  30. cat "$pid_file"
  31. }
  32. is_running() {
  33. [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
  34. }
  35. case "$1" in
  36. start)
  37. if is_running; then
  38. printf "[\\33[1;32m %s \\33[0;39m ] %s \n" "OK" "Already started!"
  39. else
  40. printf "Starting $name \n"
  41. cd "$dir"
  42. if [ -z "$user" ]; then
  43. sudo $cmd $options >> "$stdout_log" 2>> "$stderr_log" &
  44. else
  45. sudo -u "$user" $cmd $options >> "$stdout_log" 2>> "$stderr_log" &
  46. fi
  47. echo $! > "$pid_file"
  48. if ! is_running; then
  49. printf "[ \\33[1;31m %s \\33[0;39m ] %s \n" "KO" "Unable to start, see $stdout_log and $stderr_log"
  50. exit 1
  51. fi
  52. fi
  53. ;;
  54. stop)
  55. if is_running; then
  56. printf "Stopping $name.. \n"
  57. kill `get_pid`
  58. for i in {1..10}
  59. do
  60. if ! is_running; then
  61. break
  62. fi
  63. echo -n "."
  64. sleep 1
  65. done
  66. echo
  67. if is_running; then
  68. printf "[ \\33[1;31m %s \\33[0;39m ] %s \n" "KO" "Not stopped; may still be shutting down or shutdown may have failed"
  69. exit 1
  70. else
  71. printf "[\\33[1;32m %s \\33[0;39m ] %s \n" "OK" "Stopped"
  72. if [ -f "$pid_file" ]; then
  73. rm "$pid_file"
  74. fi
  75. fi
  76. else
  77. printf "$cmd is not running!"
  78. fi
  79. ;;
  80. restart)
  81. $0 stop
  82. if is_running; then
  83. printf "[ \\33[1;31m %s \\33[0;39m ] %s \n" "KO" "Unable to stop, will not attempt to start"
  84. exit 1
  85. fi
  86. $0 start
  87. ;;
  88. status)
  89. if is_running; then
  90. printf "[\\33[1;32m %s \\33[0;39m ] %s \n" "OK" "$cmd is running! - Process: $(get_pid)"
  91. else
  92. printf "$cmd is stopped!"
  93. exit 1
  94. fi
  95. ;;
  96. *)
  97. printf "Usage: $0 {start|stop|restart|status}\n"
  98. exit 1
  99. ;;
  100. esac
  101. exit 0