forked from dofl/PiCam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorageController.sh
98 lines (80 loc) · 2.71 KB
/
storageController.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
#! /bin/sh
source picam.cfg
# create the ramdisk and offline location if not found
if [ ! -d $RAMDISK ]; then
echo "Creating RAMDISK folder" >> $LOGFILE
sudo mkdir $RAMDISK
fi
if [ ! -d $OFFLINE ]; then
echo "Creating OFFLINE folder" >> $LOGFILE
sudo mkdir $OFFLINE
fi
# mount the ramdisk
if ! grep -qs $RAMDISK /proc/mounts; then
sudo mount -t tmpfs -o size=$RAMDISK_SIZE tmpfs $RAMDISK
fi
echo "$(date) - Storagecontroller startup done." >> $LOGFILE
echo "$(date) - Storagecontroller startup done. If you SCREEN'd here, ALT-D your way out."
SERVER_OFFLINE_TRIES=0
while :
do
DATE=$(date +"%Y-%m-%d %H.%M.%S")
# check the network location where the images are send to
ping -c 1 $SERVER_IP > /dev/null
if ! [ $? -eq 0 ]; then
echo "$DATE - server IP down. Umounting network folder" >> $LOGFILE
# server offline. unmount share so images go to the offline location
sudo umount -l $NETWORK
echo "$DATE - Server offline try $SERVER_OFFLINE_TRIES of 10" >> $LOGFILE
# reboot the Pi if the network failed 20 times. Network probably crashed
SERVER_OFFLINE_TRIES=$((SERVER_OFFLINE_TRIES+1))
if [[ "$SERVER_OFFLINE_TRIES" -gt 10 ]]; then
echo "$DATE - Restarting Pi. Network failed 10 times" >> $LOGFILE
sudo reboot
fi
else
# server is up. mount if neccesary
if ! grep -qs $NETWORK /proc/mounts; then
sudo mount -t cifs -o username=$SERVER_USERNAME,password=$SERVER_PASSWORD //$SERVER_IP/$SERVER_FOLDER $NETWORK
echo "$DATE - server IP up, mounted the network share" >> $LOGFILE
fi
# reset the network fail counter
SERVER_OFFLINE_TRIES=0
fi
# check if the picam script/process is still alive
sudo ps ax | grep -v grep | grep "$PICAM_SCRIPT_NAME" > /dev/null
if ! [ $? -eq 0 ]; then
echo "$DATE - PiCam down. Restarting script in 5 seconds" >> $LOGFILE
if [ $TERM_MP = "screen" ]; then
sudo screen -d -m python $PICAM_SCRIPT_LOCATION/$PICAM_SCRIPT_NAME
else
sudo tmux new-session -d -s picam-session 'python $PICAM_SCRIPT_LOCATION/$PICAM_SCRIPT_NAME'
sudo tmux detach -s picam-session
fi
sleep 10
fi
# move files to the network location if mounted, else to offline storage
if grep -qs "$NETWORK" /proc/mounts; then
for image in $(find $RAMDISK -type f -mmin +0.05); do
if ! fuser $image
then
mv $image $NETWORK
fi
done
for image in $(find $OFFLINE -type f -mmin +0.05); do
if ! fuser $image
then
mv $image $NETWORK
sleep 1
fi
done
else
for image in $(find $RAMDISK -type f -mmin +0.05); do
if ! fuser $image
then
mv $image $OFFLINE
fi
done
fi
sleep 3
done