How to set environment variables in Supervisor service - python

How do you export environment variables in the command executed by Supervisor? I first tried:
command="export SITE=domain1; python manage.py command"
but Supervisor reports "can't find command".
So then I tried:
command=/bin/bash -c "export SITE=domain1; python manage.py command"
and the command runs, but this seems to interfere with the daemonization since when I stop the Supervisor daemon, all the other daemons it's running aren't stopped.

To add a single environment variable, You can do something like this.
[program:django]
environment=SITE=domain1
command = python manage.py command
But, if you want to export multiple environment variables, you need to separate them by comma.
[program:django]
environment =
SITE=domain1,
DJANGO_SETTINGS_MODULE=foo.settings.local,
DB_USER=foo,
DB_PASS=bar
command = python manage.py command

Just do it separately:
environment=SITE=domain1
command=python manage.py command
Refer to http://supervisord.org/subprocess.html#subprocess-environment for more info.

Related

Activate python virtualenv and runserver in bat file with windows terminal

I'm trying to create a bat file to open windows terminal, activate an environment and run a server. After some attemps the best I've got is this:
wt.exe cmd -NoExit -c "c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1;cd C:\Users\me\Desktop\myProyect\;python manage.py runserver"
But windows terminal activate the environment in a tab and in other tab throw this error:
[error 0x80070002 when launching `"C:\Users\me\Desktop\myproyect\;python manage.py runserver"']
I think that maybe the command after the environment activation is not properly 'passed' to the environment... but really I don't know how to solve it.
-NoExit -c both look like powershell flags, not cmd ones. That might be a place to start.
I'd recommend just taking the entire script you've got there:
c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1;cd C:\Users\me\Desktop\myProyect\;python manage.py runserver"
and putting it into a .bat file like:
powershell -f c:\Users\me\Desktop\'myProyectVirtualenv'\virtualenv\Scripts\activate.ps1
cd C:\Users\me\Desktop\myProyect\
python manage.py runserver"
then running that with wt.exe foo.bat

Start cmd and run multiple commands in the created cmd instance

I am trying to start cmd window and then running a chain of cmds in succession one after the other in that cmd window.
something like start cmd /k pipenv shell && py manage.py runserver the start cmd should open a new cmd window, which actually happens, then the pipenv shell should start a virtual environment within that cmd instance, also happens, and the py manage.py runserver should run in the created environment but instead it runs where the script is called.
Any ideas on how I can make this work?
When issuing commands chaining them, the system sees it as first command && second command
I your case, you are giving first command to be start cmd and second command py manage.py which will do exactly that, you are issuing a cmd in a new window and if that is successful, it will initiate py in the same window you started. You should therefore escape the & with a caret in order to pass through the chain to the second command window and not initiate the chain in the current window:
start cmd /k pipenv shell ^&^& py manage.py runserver
Keep in mind that you could also just add both commands into a batch file as:
pipenv shell
py manage.py runserver
and launch it as:
start "" mybatch.cmd
Your py manage.py runserver command calling python executor in your major environment. In your case, you could use pipenv run manage.py runserver that detect your virtual env inside your pipfile and activate it to run your command. An alternative way is to use virtualenv that create virtual env directly inside your project directory and calling envname\Scripts\activate each time you want to run something inside your virtual env.

Executing shell command using docker-py

I'm trying to run a shell command with docker-py on an already-running container, but get an error:
exec: "export": executable file not found in $PATH
here's how I wrote the script:
exe = client.exec_create(container=my_container, cmd='export MYENV=1')
res = client.exec_start(exec_id=exe)
so my question is how can I run a shell command (inside the container) using docker-py?
You did it quite right. But you confused shell commands with linux executables. exec_create and and exec_start are all about running executables. Like for example bash. export in your example is a shell command. You can only use it in a shell like bash running inside the container.
Additionally what you are trying to achieve (setting a environment variable) is not going to work. As soon as your exec finishes (where you set the env var) the exec process will finish and its environment is been torn down.
You can only create global container environment variables upon creation of a container. If you want to change the env vars, you have to tear down the container and recreate it with your new vars. As you probably know, all data in the container is lost upon a remove unless you use volumes to store your data. Reconnect the volumes on container creation.
That said your example was nearly correct. This should work and create an empty /somefile.
exe = client.exec_create(container=my_container, cmd=['touch', '/somefile'])
res = client.exec_start(exec_id=exe)
To execute shell commands, use this example. It calls sh and tells it to run the interpreter on the given command string (-c)
exe = client.exec_create(container=my_container,
cmd=['/bin/sh', '-c', 'touch /somefile && mv /somefile /bla'])
res = client.exec_start(exec_id=exe)
For actually , when execute cmd docker exec in docker container export MYENV=1. It will fail and report this error
exec: "export": executable file not found in $PATH
Because export is a shell builtin, could run the cmd in shell.
whereis export
type export
can not find export in /usr/bin/ or somewhere else.
There is some ways to pass through this problem.
case1: use -c parameter
/bin/bash -c 'export MYENV=1 ; /bin/bash'
case2: append export cmds to a rcfile, then use this file.
echo "exprot MYENV=1" >> <some_file_path> ; /bin/bash --rcfile <some_file_path>
case3: open a terminal, then enter the cmds to export env parameters , then open a new terminal, the env parameters will work.
/bin/bash
exprot MYENV=1
/bin/bash # open a new terminal

Supervisor and perlbrew

I try to use supervisor with perlbrew, but I can not make it work. For perlbrew I just tried to set the environment variable that go well, but perhaps it is better to make a script that launches perlbrew and plackup, this my configuration file:
[program:MahewinSimpleBlog]
command = perlbrew use perl-5.14.2 && plackup -E deployment -s Starman --workers=10 -p 4000 -a bin/app.pl -D
directory = /home/hobbestigrou/MahewinSimpleBlog
environment = PERL5LIB ='/home/hobbestigrou/MahewinBlogEngine/lib',PERLBREW_ROOT='/home/hobbestigrou/perl5/perlbrew',PATH='/home/hobbestigrou/perl5/perlbrew/bin:/home/hobbestigrou/perl5/perlbrew/perls/perl-5.14.2/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games',MANPATH='/home/hobbestigrou/perl5/perlbrew/perls/perl-5.14.2/man:',PERLBREW_VERSION='0.43',PERLBREW_PERL='perl-5.14.2',PERLBREW_MANPATH='/home/hobbestigrou/perl5/perlbrew/perls/perl-5.14.2/man',PERLBREW_SKIP_INIT='1',PERLBREW_PATH='/home/hobbestigrou/perl5/perlbrew/bin:/home/hobbestigrou/perl5/perlbrew/perls/perl-5.14.2/bin',SHLVL='2'
user = hobbestigrou
stdout_file = /home/hobbestigrou/mahewinsimpleblog.log
autostart = true
In the log I see it's not looking at the right place:
Error while loading bin/app.pl: Can't locate Type/Params.pm in #INC (#INC contains: /home/hobbestigrou/MahewinSimpleBlog/lib /home/hobbestigrou/MahewinBlogEngine/lib /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at /home/hobbestigrou/MahewinBlogEngine/lib/MahewinBlogEngine/Article.pm line 5.
I do not see the problem, maybe perlbrew use done other things
When you installed perlbrew, you added a command to your .bashrc. You're getting that message because that command wasn't run for the shell in question because it's not an interactive shell.
Why don't you explicitly use /home/hobbestigrou/perl5/perlbrew/perls/perl-5.14.2/bin/perl instead of using perlbrew use?

From a python script, change user, set environment and run a couple of commands

I need to run a python script that changes user, sets a enviroment variable and executes a command and return the output.
1.) The way I am currently doing this is I am creating a shell script that does this for me:
tmpshell.sh
su - grid -c "echo +ASM1 | . oraenv; asmcmd volinfo -a"
The command fails because the environment is not being set.
2.) The second way I tried was by changing user is python script itself and then creating the shell script.
tmp.py
os.system('su - grid')
TMPFILE="/tmp/tmpfile.sh"
filehandle=open(TMPFILE,'w')
filehandle.write('+ASM1|. oraenv')
filehandle.write('asmcmd volinfo -a')
filehandle.close()
os.chmmod(TMPFILE,0755)
Here the problem is that the python script changes the user but the rest of the script doesn't run until I enter exit.
OUTPUT
[root#odadev1 oakvmclientlib]# python test.py
[grid#odadev1 ~]$ exit
[root#odadev1 oakvmclientlib]#
Any suggestions/better ways to do this ??
p.s.(edit) ". oraenv" is for setting the environment and +ASM1 is the environment variable it expects.
Try something like this:
$ sudo -u grid sh -c ". oraenv; echo +ASM1|asmcmd volinfo -a"
This will launch a shell as user grid, set up the environment in it and execute the command. I'm not sure what the second part of your command does, though - I suspect you want to pipe +ASM1 into the standard input of asmcmd, but you haven't given enough context to be sure.

Categories

Resources