edited dilatometer
[matches/MCTX3420.git] / server / run.sh
1 #!/bin/bash
2
3 # Check existence of program
4 if [ ! -e "server" ]; then
5         (echo "Rebuild server.") 1>&2;
6         exit 1
7 fi
8
9 # Get the parameters
10 . parameters
11 echo "Parameters are: $parameters"
12
13 if [[ "$(uname -m)" != *arm*  ]]; then
14         echo Not running on the BBB
15         # Use this to quickly test run the server in valgrind
16         #spawn-fcgi -p9005 -n ./valgrind.sh
17         # Use this to run the server normally
18         spawn-fcgi -p9005 -n -- ./server $parameters
19         exit 0
20 fi
21
22 # Check running as root
23 if [ "$(whoami)" != "root" ]; then
24         (echo "Run $0 as root.") 1>&2
25         exit 1
26 fi
27
28 # Rotate the logs
29 echo Rotating the system logs
30 logrotate -f /etc/logrotate.d/mctxserv.conf
31
32 # Identify cape-manager slots
33 slot=$(echo /sys/devices/bone_capemgr.*/slots | awk '{print $1}')
34 pwm=/sys/class/pwm/
35
36 # Load PWM module
37 if [ $(cat $slot | grep am33xx_pwm -c) -gt 0 ]; then 
38         echo am33xx_pwm already loaded, not loading again; 
39 else
40         echo Enabling PWM driver am33xx_pwm
41         echo am33xx_pwm > $slot
42 fi;
43
44 # Safe pins that won't interfere with one another
45 #ports=(P9_22 P9_42 P9_16 P8_13 P9_28);
46 #portnumbers=(0 2 4 6 7);
47 #Full correspondence from pwm0-pwm7
48 ports=(P9_22 P9_21 P9_42 P9_14 P9_16 P8_19 P8_13 P9_28);
49 portnumbers=(0 1 2 3 4 5 6 7);
50 # Enable PWM pins
51 # They must be exported at this stage, before the device tree
52 # overlay for that pin is enabled.
53 for ((c=0; c < ${#ports[*]}; c++)); do
54         if [ ! -d $pwm/pwm${portnumbers[$c]} ]; then
55                 echo Exporting PWM ${portnumbers[$c]} \(${ports[$c]}\)
56                 echo ${portnumbers[$c]} > $pwm/export
57         else
58                 echo PWM ${portnumbers[$c]} already enabled
59         fi;
60                 
61         if [ $(cat $slot | grep ${ports[$c]} -c) -gt 0 ]; then
62                 echo PWM pin ${ports[$c]} already enabled, not enabling again
63         else
64                 (echo bone_pwm_${ports[$c]} > $slot) 1>&2 >> /dev/null
65         fi;
66 done;
67
68 # Load ADCs
69 if [ $(cat $slot | grep BB-ADC -c) -gt 0 ]; then 
70         echo BB-ADC already loaded, not loading again; 
71 else 
72         echo Enabling BB-ADC
73         (echo BB-ADC > $slot) 1>&2 >> /dev/null
74 fi;
75
76 # Find adc_device_path
77 # NOTE: This has to be passed as a parameter, because it is not always the same. For some unfathomable reason. Hooray.
78 #adc_device_path=$(dirname $(find /sys -name *AIN0))
79
80
81
82
83 # Run the program with parameters
84 # TODO: Can tell spawn-fcgi to run the program as an unprivelaged user?
85 # But first will have to work out how to set PWM/GPIO as unprivelaged user
86 # NOTE: Having the program automatically restart itself after a *FATAL ERROR* doesn't seem like such a good idea now
87 # (Some things that call Fatal happen under circumstances where they will just keep calling Fatal every time the program starts)
88 # Change the number of fails to 1 for now. We can potentially use different error codes for different types of errors, but that seems overkill.
89 fails=0
90 while [ $fails -lt 1 ]; do
91         spawn-fcgi -p9005 -n -- ./server $parameters
92                 error=$?
93         if [ "$error" == "0" ]; then
94                 exit 0
95         fi
96         fails=$(( $fails + 1 ))
97         (echo "Restarting server after Fatal Error #$fails") 1>&2
98         
99 done
100 (echo "Server had too many Fatal Errors ($fails)") 1>&2
101 exit $fails

UCC git Repository :: git.ucc.asn.au