Hello everyone,
This is my first appearance to the mailing list and i am looking for a
solution regarding variable monitoring in bash scripts. What i meant by
variable monitoring is to periodically read variable values and store it
to a file for later processing. The quickest and easiest way is to use a
while loop, waiting for a while, retrieve the value and storing it to a
file. However, i am looking for an asynchronous solution such that the
parent process, where the variable resides, does not do the job because
there are many other tasks that it is responsible for. In turn i want to
spawn a child process to do the job.
The first approach i looked was to export this variable into the child
process and periodically store it from the child process. The problem is
the child process gets the variable when it is spawned and does not have
its updates as time progresses.
The second approach i looked was by trapping the *SIGCHLD* signal from
the child process after storing the variable. The parent process
trapping this signal can re-export the variable to a new child process
which again will save it to a file and exit. The process is iterative
but i couldn't go beyond two iterations. Here is an example i used by
displaying the variable values instead of saving it to a file.
_parent __process_
*#!/bin/bash**
**set -m **
**chldhandler()**
**{**
** trap 'chldhandler' SIGCHLD**
** VAR=$((VAR+1))**
** bash -c 'source ./b.sh VAR' &**
** PID=$!****
**
** wait $PID**
**
**} **
**trap 'chldhandler' SIGCHLD**
**
**export VAR**
**VAR=1**
**bash -c 'source ./b.sh VAR' &**
**PID=$!**
**
**wait $PID*
_child process
_*#!/bin/bash**
**
**element=$1**
**echo ${!element}**
**sleep 0.5*_
_I hope you understand my difficulties and i am glad if someone help
with it.
Best regards,
Michael