Gary Wu | 950a323 | 2019-03-26 13:08:29 -0700 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | ### BEGIN INIT INFO |
| 3 | # Provides: k8s_vm_init.sh |
| 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 | dir="/opt" |
| 13 | cmd="./k8s_vm_init.sh" |
| 14 | user="root" |
| 15 | |
| 16 | name=`basename $0` |
| 17 | pid_file="/var/run/$name.pid" |
| 18 | stdout_log="/var/log/$name.log" |
| 19 | stderr_log="/var/log/$name.err" |
| 20 | |
| 21 | get_pid() { |
| 22 | cat "$pid_file" |
| 23 | } |
| 24 | |
| 25 | is_running() { |
| 26 | [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1 |
| 27 | } |
| 28 | |
| 29 | case "$1" in |
| 30 | start) |
| 31 | if is_running; then |
| 32 | echo "Already started" |
| 33 | else |
| 34 | echo "Starting $name" |
| 35 | cd "$dir" |
| 36 | if [ -z "$user" ]; then |
| 37 | sudo $cmd >> "$stdout_log" 2>> "$stderr_log" & |
| 38 | else |
| 39 | sudo -u "$user" $cmd >> "$stdout_log" 2>> "$stderr_log" & |
| 40 | fi |
| 41 | echo $! > "$pid_file" |
| 42 | if ! is_running; then |
| 43 | echo "Unable to start, see $stdout_log and $stderr_log" |
| 44 | exit 1 |
| 45 | fi |
| 46 | fi |
| 47 | ;; |
| 48 | stop) |
| 49 | if is_running; then |
| 50 | echo -n "Stopping $name.." |
| 51 | kill `get_pid` |
| 52 | for i in {1..10} |
| 53 | do |
| 54 | if ! is_running; then |
| 55 | break |
| 56 | fi |
| 57 | |
| 58 | echo -n "." |
| 59 | sleep 1 |
| 60 | done |
| 61 | echo |
| 62 | |
| 63 | if is_running; then |
| 64 | echo "Not stopped; may still be shutting down or shutdown may have failed" |
| 65 | exit 1 |
| 66 | else |
| 67 | echo "Stopped" |
| 68 | if [ -f "$pid_file" ]; then |
| 69 | rm "$pid_file" |
| 70 | fi |
| 71 | fi |
| 72 | else |
| 73 | echo "Not running" |
| 74 | fi |
| 75 | ;; |
| 76 | restart) |
| 77 | $0 stop |
| 78 | if is_running; then |
| 79 | echo "Unable to stop, will not attempt to start" |
| 80 | exit 1 |
| 81 | fi |
| 82 | $0 start |
| 83 | ;; |
| 84 | status) |
| 85 | if is_running; then |
| 86 | echo "Running" |
| 87 | else |
| 88 | echo "Stopped" |
| 89 | exit 1 |
| 90 | fi |
| 91 | ;; |
| 92 | *) |
| 93 | echo "Usage: $0 {start|stop|restart|status}" |
| 94 | exit 1 |
| 95 | ;; |
| 96 | esac |
| 97 | |
| 98 | exit 0 |