-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgas_counter
executable file
·50 lines (41 loc) · 1.93 KB
/
gas_counter
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
#!/bin/sh /etc/rc.common
# https://github.com/tlinnet/gas-counter-magnetic/tree/main
START=95
STOP=15
start() {
echo "/etc/init.d/gas_counter start(): sleeping 10s" | tee -a /root/gas_data.log
sleep 10 # Wait for boot
# Update time: -n Run in foreground, -q Quit after clock is set, -p PEER Obtain time from PEER
ntpd -n -q -p openwrt.pool.ntp.org | tee -a /root/gas_data.log
now=`date +'%Y-%m-%d %I:%M:%S'`
echo "ntpd updated to: ${now}" | tee -a /root/gas_data.log
while true # Keep an infinite loop to reconnect when connection lost/broker unavailable
do
now=`date +'%Y-%m-%d %I:%M:%S'`
echo "START mosquitto_sub listening: ${now}" | tee -a /root/gas_data.log
mosquitto_sub -h slateplus.lan -u "gasread" -P "Hello" -v -t "sensors/gas/#" --qos 1 --id "gasread" --disable-clean-session | while read msg
do
echo "${msg}" >> /root/gas_data.csv
done
sleep 10 # Wait 10 seconds until reconnection
done & # The last & to run script in background
}
stop() {
now=`date +'%Y-%m-%d %I:%M:%S'`
echo "/etc/init.d/gas_counter stop(): ${now}" | tee -a /root/gas_data.log
echo "KILL mosquitto_sub listening: ${now}" | tee -a /root/gas_data.log
# Find jobs: mosquitto_sub
pids=`ps | grep 'mosquitto_sub' | grep -v grep | awk '{print $1}'`
echo "Killing 'mosquitto_sub' pids: ${pids}" | tee -a /root/gas_data.log
for pid in $pids; do kill -9 $pid; done
# Find jobs: boot script
pids=`ps | grep 'gas_counter boot' | grep -v grep | awk '{print $1}'`
echo "Killing 'gas_counter boot' pids: ${pids}" | tee -a /root/gas_data.log
for pid in $pids; do kill -9 $pid; done
pids=`ps | grep 'gas_counter start' | grep -v grep | awk '{print $1}'`
echo "Killing 'gas_counter start' pids: ${pids}" | tee -a /root/gas_data.log
for pid in $pids; do kill -9 $pid; done
pids=`ps | grep 'gas_counter restart' | grep -v grep | awk '{print $1}'`
echo "Killing 'gas_counter restart' pids: ${pids}" | tee -a /root/gas_data.log
for pid in $pids; do kill -9 $pid; done
}