2

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.

3 Answers 3

0

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

3
  • 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. Sep 26, 2013 at 1:43
  • it has useful information about cron jobs and environment variables.
    – smcg
    Sep 26, 2013 at 13:49
  • That's true, but it's applicable here. Sep 26, 2013 at 13:52
0

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

3
  • Well, yeah, but then I need to remember to run "token" every so often. That's what I'm trying to avoid. Sep 25, 2013 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. Sep 25, 2013 at 18:15
  • Ahhhhh, I had forgotten about the $(executable) trick. That's perfect, thanks! Sep 25, 2013 at 19:14
0

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.

2
  • 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. Sep 25, 2013 at 18:48
  • In Bash: if (( $(date +%s) - TOKEN_TIMESTAMP >= 600 )) Sep 26, 2013 at 1:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .