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.
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
you could create an alias to update the env var
then, simply running "token" will set that var for current session
you can add to your bash profile, so the alias remains accross logins
Store two environment variables,
TOKEN
andTOKEN_TIMESTAMP
.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.