5 # =============================================================================
7 # =============================================================================
10 prefix
='/dev/shm/khatus'
12 sensor
="$(basename $0)"
13 run_in
='foreground' # foreground | background
14 run_as
='poller' # poller | streamer
15 interval
=1 # Only relevant if run_as poller, ignored otherwise.
17 set_common_options
() {
29 printf "Option $1 requires and argument\n" >&2
42 work_dir
="${prefix}/${host}/${sensor}"
43 out_dir
="${work_dir}/out"
44 err_file
="${work_dir}/err"
45 pid_file
="${work_dir}/pid"
54 printf "%s\n" "$val" > "${out_dir}/${key}"
67 pid_file_create_of_parent
() {
68 printf "$$\n" > "$pid_file"
71 pid_file_create_of_child
() {
72 printf "$!\n" > "$pid_file"
76 if test -e "$pid_file"
78 printf "Error - $sensor already running (i.e. PID file exists at $pid_file)\n" 1>&2
88 # TODO: Why do INT and EXIT traps only work in combination?
91 trap pid_file_remove EXIT
95 run_in_background_2nd_fork
() {
96 run_in_foreground
$1 &
97 pid_file_create_of_child
100 run_in_background
() {
101 run_in_background_2nd_fork
$1 &
106 in 'poller' |
'streamer')
109 printf "Error - illegal value for \$run_as: $run_in\n" 1>&2
117 pid_file_create_of_parent
120 printf "Error - illegal value for \$run_in: $run_in\n" 1>&2
125 # =============================================================================
127 # -----------------------------------------------------------------------------
130 # =============================================================================
140 set_common_options $@