6 prefix
='/dev/shm/khatus'
8 sensor
="$(basename $0)"
9 run_in
='foreground' # foreground | background
10 run_as
='poller' # poller | streamer
11 interval
=1 # Only relevant if run_as poller, ignored otherwise.
13 set_common_options
() {
25 printf "Option $1 requires and argument\n" >&2
38 work_dir
="${prefix}/${host}/${sensor}"
39 out_dir
="${work_dir}/out"
40 err_file
="${work_dir}/err"
41 pid_file
="${work_dir}/pid"
50 printf "%s\n" "$val" > "${out_dir}/${key}"
63 pid_file_create_of_parent
() {
64 printf "$$\n" > "$pid_file"
67 pid_file_create_of_child
() {
68 printf "$!\n" > "$pid_file"
72 if test -e "$pid_file"
74 printf "Error - $sensor already running (i.e. PID file exists at $pid_file)\n" 1>&2
84 # TODO: Why do INT and EXIT traps only work in combination?
87 trap pid_file_remove EXIT
91 run_in_background_2nd_fork
() {
93 pid_file_create_of_child
97 run_in_background_2nd_fork
&
102 in 'poller' |
'streamer')
105 printf "Error - illegal value for \$run_as: $run_in\n" 1>&2
113 pid_file_create_of_parent
116 printf "Error - illegal value for \$run_in: $run_in\n" 1>&2
121 set_common_options $@