How to set global environment variables using shell script .sh - python

I have a .sh file that locally sets some environment variables in my shell source my_env.sh.
#!/bin/sh
echo "Setting up local environment variables"
export MY_URL="http://domain.com"
echo "Done"
This only sets the variables for that session. Which means that my Python Celery and Supervisor apps which run under a different session cannot access them. I understand I can run them under a user, but I want to know if on Ubuntu using the same shell script above if I can set the variables so they are globally accessible to all applications regardless of users or session?

export your variables in the "/etc/profile".
NOTE: This will make it global for each shell sessions for any user. If you wish to set this variable for every session for a specific user, set it in that user's "~/.profile".

According to Ubuntu env variables doc the best way would be
A suitable file for environment variable settings that affect the system as a whole (rather than just a particular user) is /etc/environment
That's assuming you don't mind having them set for the whole machine.

If you are using bashrc or zshrc you can source the shell file which sets the environment variables across the sessions or precisely loads all the environment variables in each session.
source location/shell-script-sets-env.sh
The zshrc and bashrc is available in your $HOME directory. or ~/.zshrc and ~/.bashrc. The current shell can be looked via
echo $SHELL
Have a look at setting env permanently for adding it to the profile.

Related

python script doesn’t pick up environment variable that is set in crontab

When i run my python script from terminal i get error due to missing env variable. i can fix by this using export and setting it. But there are other python scripts that are running fine as cron jobs that require this as well. And i see this env variable being set in cron tab. So my question is if env variable set in cron is available for scripts run by user/root from cli? Running env command as root doesn’t show this variable.
Environment variables are private to each process, not global. When a new process is started it inherits its initial environment from its parent. Your terminal shell isn't started by cron so it doesn't inherit the env vars you've set in your crontab.
Your OS may provide a standard mechanism for setting env vars for all processes (e.g., your terminal which then starts your CLI shell). What I prefer to do is create a file named ~/.environ that I then source in my ~/.bashrc and cron jobs.

How to grant access to virtual environment to other users on CentOS server

I used this tutorial (rec by GoDaddy because I have a GoDaddy VPS)
https://www.godaddy.com/garage/how-to-install-and-configure-python-on-a-hosted-server/
to install a secondary version of Python (3.6.6) and create a Virtual Environment to run modules with the secondary version of Python. I want to be able to run scripts in the virtual environment but do not want to always use the admin account to do this. I have one other account set up that does not have admin privileges. Is there a way to extend authority to more than the admin account that created it? Do I need to do a virtual environment on every user account that is going to have access to run python?
It says to add a line to the bash_profile script. The script already had this:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
This makes sense in that it seems to be defining the location of underlying Python (i.e., 2.6.6). I have modules in a venv that require 3.4+ so I created the virtual environment. I want to be able to call the script from Chrome and run it for some scripts and run it from cronjob for other scripts and the scripts need to always point to the virtualenv to use that installed interpreter. I have no idea how to do this correctly. Here's my system information and the results of some SSH printouts:
$ cd $PATH
-bash: cd: /home/flohosti/.local/bin:/home/flohosti/perl5/bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/cpanel/3rdparty/lib/path-bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:/opt/cpanel/composer/bin: No such file or directory
Before going into the VirtualEnv I run which python and get /usr/bin/python which I expected.
There are multiple ways to do that.
You can simply give read access to the directory where virtual environment is installed and then export its path in /etc/profile so that its automatically added to every users path.
Or you can give access to python binary inside the virtual env folder.
That being said this isn't the right way to go about it, most of the times you would want to have virtual environment inside your projet folder so that it is automatically available the project and who so ever has access to that project.
Edit 1
So the answer to your comment
Yes absolutely you can use the same virtual env to execute script in cron but you have to give absolute path to virtaul environment and your script in cron, like /home/flohosti/projectOne/env/bin/python <script path> or you can just set the PATH in cron and then you can use relative path as well.

How to access system environment variables using Python

I am aware that you can use os.environ['key'] to access an environment variable, but it appears that this function is localized to environment variables for the signed in user. I need to access the system environment variables. Is this possible?
You cannot do this using os.environ in python. You can, however, use os.system to run a commands such as:
sudo -Hiu $user env | grep $var
to access a variable from another user. This is a bit hacky, but it works. And your options are limited here.
Also -- why do you want to? You should have a process for setting the environment variables before this script is run at all, ideally.

How do I set an environment variable for airflow to use?

Airflow is returning an error when trying to run a DAG saying that it can't find an environment variable, which is odd because it's able to find 3 other environment variables that I'm storing as a Python variable. No issues with those variables at all.
I have all 4 variables in ~/.profile and have also done
export var1="varirable1"
export var2="varirable2"
export var3="varirable3"
export var4="varirable4"
Under what user does airflow run? I've done those export commands under sudo as well, so I thought they would be picked up by airflow when it runs the dag
Is it maybe because airflow uses non-login shell? Have you tried putting these lines in : ~/.bashrc instead of ~/.profile ?
As per this answer, the variables should be put in /etc/default/airflow (on Debian/Ubuntu) or /etc/sysconfig/airflow (on Centos/Redhat).
If you are just running a local instance you should be able to use environment variables like you expect. Remember that you need to set them in the shell that is running the webserver and scheduler though. If these are in your .profile, you may need to run source ~/.profile.

Access macOS environment variables from Python

I got a bunch of environment variables set with the launchctl command:
launchctl setenv TEST /Users/JohnDoe/Test
To get the value back, I can use:
launchctl getenv TEST
However, I can't access the value of TEST from Python, using os.getenv('TEST') (or even from Bash using echo $TEST). I do know how macOS manages environment variables (the difference between launchctl and Bash environment variables, etc.) so I understand why those commands don't return the value of TEST.
My question is: is there a way to access environment variables set with launchctl, without using subprocess? Using subprocess is not a no-go, I'd just rather avoid throwing lots of processes just to get environment variables :)

Categories

Resources