-
Notifications
You must be signed in to change notification settings - Fork 9
/
administration.sh
107 lines (93 loc) · 2.33 KB
/
administration.sh
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
#!/bin/bash
# Sensor21: MPL3115A2 Breakout Board Administration Script
#
# This script provides a few useful functions for working with
# system processes related to the MPL3115A2 sensor21 implementation.
# Note: pigpiod is only used for software I2C (used with 21 Bitcoin Computer)
# Usage: bash administration.sh <function>
# bash administration.sh start_server
# bash administration.sh restart_pigpiod
# function list
# start_pigpiod, stop_pigpiod, restart_pigpiod
# start_server, stop_server, restart_server
# start_cron_job, stop_cron_job
## Process names
PIGPIOD="pigpiod"
SERVER="sensor21-server.py"
## Helper functions
# Check to see if a process is running
# Returns 0 if yes, 1 if no
check_process () {
pgrep -f $1 > /dev/null
return $?
}
# Stop process
stop_process () {
proc_num=$(ps aux | grep -v grep | pgrep -f $1)
sudo kill $proc_num > /dev/null
}
## PIGPIOD management
# start pigpiod
start_pigpiod () {
if ! check_process $PIGPIOD; then
sudo pigpiod
fi
}
# stop pigpiod
stop_pigpiod () {
if check_process $PIGPIOD; then
stop_process $PIGPIOD
fi
}
# restart pigpiod
restart_pigpiod () {
stop_pigpiod
start_pigpiod
}
## Server Management
# start server
start_server () {
start_pigpiod
if ! check_process $SERVER; then
python3 sensor21-server.py &
fi
}
# stop server
stop_server () {
if check_process $SERVER; then
stop_process $SERVER
fi
}
# restart server
restart_server () {
stop_server
start_server
}
## Crontab management
# start cron job
start_cron_job () {
crontab -l 2>&1 | grep '* * * * * /usr/bin/python3 /home/twenty/sensor21/cron.py >> /home/twenty/sensor21/cron_log.txt 2>&1' > /dev/null
if [ "$?" = "1" ]; then
crontab cron.txt
fi
}
# stop cron job
stop_cron_job () {
crontab -l 2>&1 | grep '* * * * * /usr/bin/python3 /home/twenty/sensor21/cron.py >> /home/twenty/sensor21/cron_log.txt 2>&1' > /dev/null
if [ "$?" = "0" ]; then
crontab -r
fi
}
# Print available functions if user runs with no arguments.
if [ "$1" = "" ]; then
echo "usage: source administration.sh <FUNCTION>"
echo "Note: pigpiod is only used for software I2C (used on 21 Bitcoin Computer)"
echo ""
echo "Supported functions:"
echo "start_pigpiod, stop_pigpiod, restart_pigpiod"
echo "start_server, stop_server, restart_server"
echo "start_cron_job, stop_cron_job"
else
# Run function calls from script arguments
$@
fi