summarylogtreecommitdiffstats
path: root/oml2-server.rc
blob: efbc6147fbcc423c10dc991331dcfb6a5bf298a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash

daemon_name=oml2-server
pidfile=/run/$daemon_name.pid

. /etc/rc.conf
. /etc/rc.d/functions
. /etc/conf.d/$daemon_name.conf

get_pid() {
  pidof -o %PPID $daemon_name
}

test_pid(){
  if [ -s "$pidfile" ]; then
	if pidof -o %PPID $daemon_name | tr ' ' '\n' | grep -w $(cat $pidfile); then
      return 0
    fi
  fi
  return 1
}

case "$1" in
	start)
		stat_busy "Starting $daemon_name daemon"

		PID=$(get_pid)
		if [[ -z $PID ]]; then
			[[ -f $pidfile ]] &&
				rm -f $pidfile
		# RUN
		$daemon_name $OML2_OPTS & 
		#
		if [[ $? -gt 0 ]]; then
			stat_fail
			exit 1
		else
			echo $(get_pid) > $pidfile
			add_daemon $daemon_name
			stat_done
		fi
		else
			stat_fail
			exit 1
		fi
		;;

	stop)
		stat_busy "Stopping $daemon_name daemon"
		PID=$(get_pid)
		# KILL
		[[ -n $PID ]] && kill $PID &> /dev/null
		#
		if [[ $? -gt 0 ]]; then
			stat_fail
			exit 1
		else
			rm -f $pidfile &> /dev/null
			rm_daemon $daemon_name
			stat_done
		fi
		;;

	restart)
    $0 wait_stop
		$0 start
		;;

	status)
		stat_busy "Checking $daemon_name status";
		ck_status $daemon_name
		;;

  wait_stop)
    pidtmp=$(test_pid) || exit 0
    if kill -0 "${pidtmp:-}" 2> /dev/null; then
      PID=$pidtmp
    fi

    $0 stop

    if [ -n "${PID:-}" ]; then
      i=0
      stat_busy "Waiting for server to terminate"
      while kill -0 "${PID:-}" 2> /dev/null;  do
        if [ $i = '60' ]; then
          stat_fail
          break;
        else
          i=$(($i+1))
          sleep 1
        fi
      done
      stat_done
    fi
    ;;

	*)
		echo "usage: $0 {start|stop|restart|status|wait_stop}"
esac

exit 0

# vim:set ts=2 sw=2 et ft=sh: