Your Location is: Home > Linux

Non-persistent system wide environment variables

From: Male View: 3256 Rune79 

Question

I have been looking for a solution to setup an environment variable, that is non-persistent after reboot, but still accessable system wide. Anyone with a solution (bash)?

The reason is that I want crontab etc. to use these variables (after they have been set once in a single session). But, if the power goes or the disk is taken physically the variable (a password) is not found in the file system. (It's a Raspberry Pi backup server). So far I have considered using Screen (with no real luck), and is considering a script in /etc/profile.d

Best answer

The reason is that I want crontab etc. to use these variables (after they have been set once in a single session). But, if the power goes or the disk is taken physically the variable

So store the password in memory, assuming that memory is cleared after power loss. Ex. use shared memory or tmpfs filesystem. First script to setup the password:

#!/bin/bash
# setup_password.sh
if ! findmnt /srv/mypassword >/dev/null; then
      mount -o tmpfs tmpfs /srv/mypassword
fi
printf "%s\n" "$1" > /srv/mypassword/password.txt

and script you execute from cron:

#!/bin/bash

if ! password=$(cat /srv/mypassword/password.txt); then
     echo "ERROR: password not setup" >&2
     exit 2
fi
: continue and use the password "$password"

If your /tmp is a tmpfs, and probably it is, just store the password in /tmp...