summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.remote.utils/resources/yocto_tool.sh
blob: 099e481543c7e3466be6e1c8bcfeda1a1ac289aa (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh

help ()
{
  echo "Usage $0 command [options] application [application argument]"
  echo "command:"
  echo "  start - start an application"
  echo "  stop - stop an application"
  echo "  restart - restart an application"
  echo ""
  echo "options: -d | -l <log file>"
  echo "  -d  - start an application as a singleton daemon"
  echo "  -l <log file name> - redirect the standard output/error in the the file" 
  echo "  note: Option -d and -l are exclusive to each other"
  exit 1
}

killproc() {        # kill the named process(es)
    pid=`/bin/pidof $1`
    [ "x$pid" != "x" ] && kill $pid
}

start () 
{
        pid=`/bin/pidof $APP`
        [ "x$pid" != "x" ] && return 0
        
        if [ "x$DAEMON" != "x" ]; then
            if [ "x$APPARG" != "x" ]; then
                start-stop-daemon -S -b --oknodo -x $APP -- $APPARG
            else
                start-stop-daemon -S -b --oknodo -x $APP
            fi
            
            #wait for sometime for the backend app to bring up & daemonzie
            ret=$?
            if [ $ret -eq 0 ]; then
               sleep 1
            fi
            return $ret           
        elif [ "x$LOGFILE" != "x" ]; then
            $APP $APPARG $>${LOGFILE}
        else
            $APP $APPARG
        fi
}

stop ()
{
  if [ "x$DAEMON" != "x" ]; then
    start-stop-daemon -K -x $APP
  else
    count=0
    while [ -n "`/bin/pidof $APP`" -a $count -lt 10 ] ; do
      killproc $APP >& /dev/null
      sleep 1
      RETVAL=$?
      if [ $RETVAL != 0 -o -n "`/bin/pidof $APP`" ] ; then
          sleep 3
      fi
      count=`expr $count + 1`
    done
  fi
}

restart ()
{
  stop
  sleep 1
  start
}

#set PATH to include sbin dirs
export PATH="$PATH:/usr/local/sbin:/usr/sbin:/sbin"

#get command
case $1 in 
start) CMD=$1; shift 1
    ;;
stop)  CMD=$1; shift 1
    ;;
*)  help
    ;;
esac

#get options
while [ $# -gt 0 ]; do
  case $1 in
  -d) DAEMON=true; shift 1
    ;; 
  -l) LOGFILE=$2; shift 2
    ;;
  *) break
    ;;
  esac
done

#get application
APP=$1
shift 1

#get app argument
APPARG="$@"

#validate options
if [ "x$DAEMON" != "x" -a "x$LOGFILE" != "x" ]; then
  help
fi
if [ "x$DAEMON" != "x" ]; then
  APP=`which $APP`
fi
if [ "x$APP" == "x" ]; then
  help
fi

#run script
case $CMD in 
start) start
    ;;
stop) stop
    ;;
restart)
    restart
    ;;
esac