Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I regularly invoke a particular remote server from a (Linux/bash) command line via tools like cURL or wget. This server requires an authentication token that expires every 10 minutes. I have a program that can generate a new token.

What I would like is an environment variable, $TOKEN, that I can use from the command line, that refreshes itself every 10 minutes, or, better yet, refreshes itself only when requested, and even then only every 10 minutes at most.

I was hoping that there was a way to tie an environment variable's evaluation to an executable, allowing me to do so with a script. Failing that, I was wondering if perhaps there was a way to set up a background process that updated the environment variable every 10 minutes.

share|improve this question

3 Answers 3

You could set up a cron job that calls a script every 10 minutes (or whatever time interval you want). Then the script updates the variable.

See: linux: how to permanently and globally change environment variables

share|improve this answer
    
I would recommend this, but use a file to store the value since variables set in the cron job would only be available to its children and not to arbitrary processes. The question and answers you linked to aren't applicable to this question. –  Dennis Williamson Sep 26 '13 at 1:43
    
it has useful information about cron jobs and environment variables. –  smcg Sep 26 '13 at 13:49
    
That's true, but it's applicable here. –  Dennis Williamson Sep 26 '13 at 13:52

you could create an alias to update the env var

alias token='TOKEN=$(wget -q -O - http://webserver.com/TOKEN)'

or

alias token='TOKEN=$(/path/to/token-generator)'

then, simply running "token" will set that var for current session

you can add to your bash profile, so the alias remains accross logins

share|improve this answer
    
Well, yeah, but then I need to remember to run "token" every so often. That's what I'm trying to avoid. –  Brandon Yarbrough Sep 25 '13 at 17:48
2  
if you write a script that returns the token and checks for invalid/old ones, then prints it to stdout, you can invoke it as $(scriptname). If you wish to optimize out the script call, you may be able to build a bash function that has the same effect but checks the age of the cookie first (in a different variable). That variable wouldn't be shared across different scripts though, unless it was reading from a shared file of some sort. –  Andrew Domaszek Sep 25 '13 at 18:15
    
Ahhhhh, I had forgotten about the $(executable) trick. That's perfect, thanks! –  Brandon Yarbrough Sep 25 '13 at 19:14

Store two environment variables, TOKEN and TOKEN_TIMESTAMP.

if [ $(($(date +%s) - $TOKEN_TIMESTAMP)) -ge 600 ]; then
  /script/to/update/token.sh
  TOKEN_TIMESTAMP=$(date +%s) # this should be in the above script.
fi

/script/that/uses/token.sh # everything could be in this one script.

This way you don't have to store logic in an environment variable [ew] or set up a cron job. The token is refreshed on-demand.

share|improve this answer
    
Hrm...I was hoping that the token would just be available for use with arbitrary, ad-hoc commands. Perhaps I could trigger a script like this on every new prompt, but ew. –  Brandon Yarbrough Sep 25 '13 at 18:48
    
In Bash: if (( $(date +%s) - TOKEN_TIMESTAMP >= 600 )) –  Dennis Williamson Sep 26 '13 at 1:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.