set env variable in bash for t seconds

177 views Asked by At

Is there a way to set env variable for certain number of seconds and then reset. I have a script that reads the env variable. If it is set, script should exit. Basically I am trying to build a snooze algorithm. eg. It keeps on sending messages as it receives it. However sometimes I need to snooze them for say t seconds, during these t seconds it would buffer all incoming messages and when timer t expires, send the buffer contents. My approach is to use an

if [ $MY_ENV=set ]
  then 
  buffer
fi

However this env variable must not be shell specific. It should be for every user logged in from different locations on the same machine.

2

There are 2 answers

1
Mark Reed On BEST ANSWER

Regardless of the timing part, there's no way to set a variable in the environment of an already-running process from outside that process.

You need to use the file system for this. If it's user-specific, it can be a file in the user's home directory, for example.

0
chiastic-security On

If it shouldn't be shell specific, I'd use some kind of lock file for this purpose. If the lock file exists, that's the equivalent of your var being set. You can have something scheduled to delete it after t seconds.

Look at the lockfile command.