subprocess - Python and environment variables -
in following code snippet (meant work in init.d environment) execute test.classpath. however, i'm having trouble setting , passing classpath environment variable defined in user's .bashrc.
here source of frustration:
- when below script run in use mode, prints out classpath ok (from $home/.bashrc)
- when run root, displays classpath fine (i've set /etc/bash.bashrc classpath)
- but when "sudo script.py" (to simulate happens @ init.d startup time), classpath missing !!
the classpath quite large, i'd read file .. $home/.classpath
#!/usr/bin/python import subprocess import os.path osp import os user = "username" logdir = "/home/username/temp/" print os.environ["home"] if "classpath" in os.environ: print os.environ["classpath"] else: print "missing classpath" proclog = open(osp.join(logdir, 'test.log'), 'w') cmdstr = 'sudo -u %s -i java test.classpath'%(user, ) # run in user proc = subprocess.popen(cmdstr, shell=true, bufsize=0, stderr=proclog, stdout=proclog) proclog.close()
you put source ~/.bashrc before starting python script environment variables set.
Comments
Post a Comment