0

I want to source this /etc/apache2/envvars before running apache2 -V command which is giving me this output wihtout soruce

user@ubuntu16:~$ apache2 -V
[Tue Sep 14 05:01:39.045086 2021] [core:warn] [pid 13942] AH00111: Config variable ${APACHE_RUN_USER} is not defined
[Tue Sep 14 05:01:39.045115 2021] [core:warn] [pid 13942] AH00111: Config variable ${APACHE_RUN_GROUP} is not defined
[Tue Sep 14 05:01:39.045142 2021] [core:warn] [pid 13942] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Tue Sep 14 05:01:39.053712 2021] [core:warn] [pid 13942] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Tue Sep 14 05:01:39.053860 2021] [core:warn] [pid 13942] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
[Tue Sep 14 05:01:39.053878 2021] [core:warn] [pid 13942] AH00111: Config variable ${APACHE_LOG_DIR} is not defined
AH00543: apache2: bad user name ${APACHE_RUN_USER}

running the command source /etc/apache2/envvars from the terminal is making the above warn go away but I want to do this from the python script as I am calling the apache2 -V from my python script to do some parsing of the output.

I am using the command execfile to source the file but I am getting the error and not sure how to resolve this issue. Is there any right wayt to soruce this /etc/apache2/envvars file within python script

root@ubuntu16:/home/virsec# python
Python 2.7.12 (default, Mar  1 2021, 11:38:31)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> execfile("/etc/apache2/envvars")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/etc/apache2/envvars", line 4
    unset HOME
             ^
SyntaxError: invalid syntax
>>>
Aryaman Gupta
  • 616
  • 1
  • 8
  • 20

1 Answers1

1

execfile is for executing Python code, not for shell variables. I guess you found this answer. What it meant is not to execfile the envvars file, but to execfile some Python code, passing the required variables in the globals parameter. Cf Python 2 docs.

But you can't source from Python because it is a shell built-in. cf this question

Can you consider running this command instead :

$ source envvars && python so69188563.py

with

# file: envvars
export ARYAMAN=1718
# file: so69188563.py
import subprocess

outcome = subprocess.run(["python", "fake_apache2.py"], stdout=subprocess.PIPE)
if outcome.returncode != 0:
    print("error !")
else:
    print(outcome.stdout)  # do something with it
# file: fake_apache2.py
import os
print(os.environ.get("ARYAMAN", "MISSING!"))  # print the value or missing

or put it in a file :

# !/bin/sh
# file: run.sh
source envvars
echo $ARYAMAN
python so69188563.py

and execute it normally : ./run.sh

Either way you get b'1718\n' which proves that fake_apache2.py sees the envvar.


Some details on my solution :

  • I assumed you did not want to set them all in the command line, like export ARYAMAN=1718; python so69188563.py
  • You can't do something like source envvar && python so69188563.py because of the shell uses subprocess for each individual commands, they do not share environment variables (cf this question but the answers are bad)
  • The variable in the envvar must be exported, cf this question
  • I did not want to parse (Ba)sh syntax like this question does
Lenormju
  • 4,078
  • 2
  • 8
  • 22