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