functions.initscripts 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*-Shell-script-*-
  2. #
  3. # functions This file contains functions to be used by most or all
  4. # shell scripts in the /etc/init.d directory.
  5. #
  6. NORMAL="\\033[0;39m" # Standard console grey
  7. SUCCESS="\\033[1;32m" # Success is green
  8. WARNING="\\033[1;33m" # Warnings are yellow
  9. FAILURE="\\033[1;31m" # Failures are red
  10. INFO="\\033[1;36m" # Information is light cyan
  11. BRACKET="\\033[1;34m" # Brackets are blue
  12. # NOTE: The pidofproc () doesn't support the process which is a script unless
  13. # the pidof supports "-x" option. If you want to use it for such a
  14. # process:
  15. # 1) If there is no "pidof -x", replace the "pidof $1" with another
  16. # command like(for core-image-minimal):
  17. # ps | awk '/'"$1"'/ {print $1}'
  18. # Or
  19. # 2) If there is "pidof -x", replace "pidof" with "pidof -x".
  20. #
  21. # pidofproc - print the pid of a process
  22. # $1: the name of the process
  23. pidofproc () {
  24. # pidof output null when no program is running, so no "2>/dev/null".
  25. pid=`pidof $1`
  26. status=$?
  27. case $status in
  28. 0)
  29. echo $pid
  30. return 0
  31. ;;
  32. 127)
  33. echo "ERROR: command pidof not found" >&2
  34. exit 127
  35. ;;
  36. *)
  37. return $status
  38. ;;
  39. esac
  40. }
  41. machine_id() { # return the machine ID
  42. awk 'BEGIN { FS=": " } /Hardware/ \
  43. { gsub(" ", "_", $2); print tolower($2) } ' </proc/cpuinfo
  44. }
  45. killproc() { # kill the named process(es)
  46. pid=`pidofproc $1` && kill $pid
  47. }
  48. status() {
  49. local pid
  50. if [ "$#" = 0 ]; then
  51. echo "Usage: status {program}"
  52. return 1
  53. fi
  54. pid=`pidofproc $1`
  55. if [ -n "$pid" ]; then
  56. echo "$1 (pid $pid) is running..."
  57. return 0
  58. else
  59. echo "$1 is stopped"
  60. fi
  61. return 3
  62. }
  63. success() {
  64. echo -n -e "${BRACKET}[${SUCCESS} OK ${BRACKET}]${NORMAL}"
  65. return 0
  66. }
  67. failure() {
  68. local rc=$*
  69. echo -n -e "${BRACKET}[${FAILURE} FAIL ${BRACKET}]${NORMAL}"
  70. return $rc
  71. }
  72. warning() {
  73. local rc=$*
  74. echo -n -e "${BRACKET}[${WARNING} WARN ${BRACKET}]${NORMAL}"
  75. return $rc
  76. }
  77. passed() {
  78. local rc=$*
  79. echo -n -e "${BRACKET}[${SUCCESS} PASS ${BRACKET}]${NORMAL}"
  80. return $rc
  81. }