blob: 1536077399d67a8a0b23eb233aae534b216654f8 [file] [log] [blame]
Gary Wu950a3232019-03-26 13:08:29 -07001#!/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
12dir="/opt"
13cmd="./k8s_vm_init.sh"
14user="root"
15
16name=`basename $0`
17pid_file="/var/run/$name.pid"
18stdout_log="/var/log/$name.log"
19stderr_log="/var/log/$name.err"
20
21get_pid() {
22 cat "$pid_file"
23}
24
25is_running() {
26 [ -f "$pid_file" ] && ps `get_pid` > /dev/null 2>&1
27}
28
29case "$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 ;;
96esac
97
98exit 0