summaryrefslogtreecommitdiff
path: root/recipes-connectivity/lora/lora-network-server-mtcap/lora-network-server.init
blob: 935703038f708e39cc817b63a72b66fbcf950e09 (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
#!/bin/bash

NAME="lora-network-server"
LOCK="/var/lock/$NAME"
ENABLED="yes"

START_STOP_DAEMON="/usr/sbin/start-stop-daemon"

[ -f /etc/default/$NAME ] && source /etc/default/$NAME

run_dir=/var/run/lora
conf_dir=/var/config/lora
conf_file=/opt/lora/lora-network-server.conf

net_server=/opt/lora/lora-network-server
net_server_log=/var/log/lora-network-server.log
net_server_pidfile=$run_dir/$NAME.pid

pkt_fwd=/opt/lora/lora_pkt_fwd
pkt_fwd_pidfile=$run_dir/lora-pkt-fwd-1.pid

do_start() {
    if ! [ -f $conf_file ]; then
        echo "$0: $conf_file missing"
        exit 1
    fi

    echo -n "Starting $NAME: "
    mkdir -p $run_dir/1
    # start network server
    $START_STOP_DAEMON --start --background --make-pidfile \
        --pidfile $net_server_pidfile --startas /bin/bash \
        --chdir "/opt/lora" -- -c "exec $net_server \
        -c $conf_file --lora-path $run_dir --db $conf_db \
        --noconsole -l $net_server_log  >> $net_server_log 2>&1"
    sleep 2
    # start packet forwarder
    $START_STOP_DAEMON --start --background --make-pidfile \
        --pidfile $pkt_fwd_pidfile --exec $pkt_fwd --chdir "/opt/lora/"

    renice -n -20 -p $(pgrep lora-network-se)
    renice -n -20 -p $(pgrep $(basename $pkt_fwd))

    echo "OK"
}

do_stop() {
    echo -n "Stopping $NAME: "
    $START_STOP_DAEMON --stop --quiet --oknodo --pidfile $net_server_pidfile --retry 15
    $START_STOP_DAEMON --stop --quiet --oknodo --pidfile $pkt_fwd_pidfile --retry 5
    rm -f $net_server_pidfile $pkt_fwd_pidfile
    echo "OK"
}

if [ "$ENABLED" != "yes" ]; then
    echo "$NAME: disabled in /etc/default"
    exit
fi

force_stop() {
    do_stop
    rm -fr $LOCK
}

function try_lock() {
    if mkdir $LOCK; then
        trap "rm -fr $LOCK" EXIT
    else
        echo "Lora Network Server lock not acquired, resource in use"
        exit 1
    fi
}


case "$1" in
    "start")
	try_lock
        do_start
        ;;
    "stop")
	force_stop
        ;;
    "restart")
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
	try_lock
        do_stop
        do_start
        ;;
    *)
        ## If no parameters are given, print which are avaiable.
        echo "Usage: $0 {start|stop|restart}"
        exit 1
        ;;
esac