1

I have a task to monitor the system with a quota, if the monitored result is over the quota, send a warning email. But this monitor program should be called once in half an hour, after one warning email is sent out, the next time if the monitored state is still the same as last time, there is no need to send the same warning email again.

In order to do this, I would like to make use of environment variable to store the state of the last monitored result, so that the next time it can be checked and duplicate email would not be sent. One of my solution is to add or update the export syntax in .bashrc, but in order to activate the updated export syntax, I have to run bash, which might be unnecessary.

So I would like ask is there any way to update the environment variable so that every time when the monitor program Bash script is called, it gets the fresh updated value?

Rui
  • 3,454
  • 6
  • 37
  • 70
  • Why don't you store this info in a specific file (not in `.bashrc`) and you keep updating its value? – fedorqui Aug 31 '16 at 09:41
  • It is only one value, why should I store it in a specified file? and the specified file might be deleted by accident – Rui Aug 31 '16 at 09:45
  • 1
    First of all, note that running `bash` is not necessary. To load the values in `.bashrc` you just have to run `source ~/.bashrc`. This being said, I do think that variables from a program should be in a different place, not to confuse: for example `/usr/bin/myprogram/vars`. – fedorqui Aug 31 '16 at 10:05
  • @fedorqui Great hint from u :) I think I will make use of a file, i.e. log file – Rui Aug 31 '16 at 10:37
  • @fedorqui I like your response, but suggest against using `/usr/bin` as a starting location. Consider something under `/var`. – Eric Aug 31 '16 at 12:37
  • @Eric this is a good point. Checking [The Linux Filesystem -> Locations](http://swift.siphos.be/linux_sea/linuxfs.html) I see that `/var` could well be, but also `/usr` may. Not sure about what is the generic approach. – fedorqui Aug 31 '16 at 12:47

2 Answers2

0

Store environment variable in different .file and then source <.file>

0

This is a self contained solution using a heredoc. At first glance it may seem an elaborate and inperfect solution, it does have its uses in that it's resilient and it works well when deploying across more than one machine, requires no special monitoring or permissions of external files, and most importantly, there are no unwanted surprises with environment.

This example uses bash, but it will work with sh if the $thisfile variable is set manually, or another way.

This example assumes that 20 is already in the script file as mymonitorvalue, and uses argument $1 as a proof of concept. You would obviously change newval="$1" to whatever calculates the quota:


Example usage:

#bash $>./script 10
Value from previous run was 20
Value from this test was 10
Updating value ...

#bash $>./script 10
not doing anything ... result is the same as last time

#bash $>

Script:

#!/bin/bash

thisfile="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" ; thisfile="${DIR}${0}"

read -d '' currentvalue <<'EOF'
mymonitorval=20
EOF

eval "$currentvalue"

function changeval () { 
sed -E -i "s/(^mymonitorval\=)(.*)/mymonitorval\="$1"/g" "$thisfile"
}

newvalue="$1"

if [[ "$newvalue" != "$mymonitorval" ]]; then 
    echo "Value from previous run was $mymonitorval"
    echo "Value from this test was "$1""
    echo "Updating value ..."
    changeval "$newvalue"
else
    echo "not doing anything ... result is the same as last time" 
fi

Explanation:

thisfile= can be set manually for script location. This example uses the automated solution from here: https://stackoverflow.com/a/246128

read -d...EOF is the heredoc which is saved into variable $currentvalue

eval "$currentvalue" in this case is the equivalent of typing mymonitorval=10 into a terminal

function changeval...} updates the contents of the heredoc in place (it changes the physical .sh script file)

newvalue="$1" is for the purpose of testing. $newvalue would be determined by whatever your script is that is calculating quota

if.... block is to perform two alternate sets of actions depending on whether $newvalue is the same as it was last time or not.

Community
  • 1
  • 1
hmedia1
  • 5,552
  • 2
  • 22
  • 27