-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.sh
executable file
·64 lines (52 loc) · 1.76 KB
/
run.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
#!/usr/bin/env bash
set -o errexit # exit immediately upon error
set -o pipefail # exit if any step in a pipeline fails
#set -o xtrace # print each command before execution
# The presence of this file will disable the shutdown at the end.
# Create this file to disable shutdown.
SHUTDOWN_DISABLE_FILE=$HOME/noshutdown
# Store first parameter in a variable, which should be the log file location.
LOG_FILE="$1"
# Set a default log file location if the parameter was empty, i.e. not specified.
if [ -z "$LOG_FILE" ]
then
LOG_FILE="$HOME/moonpi.log"
fi
trap cleanup SIGINT SIGTERM ERR
cleanup() {
trap - SIGINT SIGTERM ERR
echo "run.sh exited abnormally" >> "$LOG_FILE"
}
# Rotate log file if necessary
MAX_LOG_SIZE=10000
if [ -f "$LOG_FILE" ]
then
log_size=$(du -k "$LOG_FILE" | cut -f 1)
if [ $log_size -ge $MAX_LOG_SIZE ]; then
# rollover log file
savelog -n -l -c 2 "$LOG_FILE"
fi
fi
echo "----------------------------------------" >> "$LOG_FILE"
echo "System date and time: $(date '+%Y/%m/%d %H:%M:%S')" >> "$LOG_FILE"
echo "Kernel info: $(uname -rmv)" >> "$LOG_FILE"
echo "Uptime: $(uptime)" >> "$LOG_FILE"
# Activate pyenv and the virtual environment
export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
pyenv activate moonpi &>> "$LOG_FILE"
# Run the moon_pi script
cd /home/moon/Moon-Pi
python moon_pi.py &>> "$LOG_FILE"
if [ -f "$SHUTDOWN_DISABLE_FILE" ]; then
echo "$SHUTDOWN_DISABLE_FILE found. Disabling auto-shutdown." >> "$LOG_FILE"
exit 0
else
# revert trap
trap - SIGINT SIGTERM ERR
echo "Shutting down system in 10 seconds." >> "$LOG_FILE"
sleep 10
sudo /usr/bin/systemctl --no-block --no-wall halt
fi