pythonRcpp
pythonRcpp

Reputation: 2146

set env variable in bash for t seconds

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.

Upvotes: 1

Views: 206

Answers (2)

Mark Reed
Mark Reed

Reputation: 95315

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.

Upvotes: 1

chiastic-security
chiastic-security

Reputation: 20520

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.

Upvotes: 1

Related Questions