|
From: | Michael Mehari |
Subject: | How to monitor bash variables periodically |
Date: | Mon, 26 Jan 2015 16:29:04 +0100 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 |
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 |
[Prev in Thread] | Current Thread | [Next in Thread] |