0

I have a python script (using a pseudo-terminal) to pass an environment variable called "CDP":

def download(self, dPluzz, donnees=None):        # to call the bash script
    self.child_pid = self.v.fork_command(None, ['/bin/bash', 'dPluzz-cli', '-f', dest, '-u', adresse])   
    os.environ["CDP"] = "False"                  # set cancel as "False"
def cancel(self, dPluzz, donnees=None):
    if self.annul == 0:
      if self.time > 10 and self.percent != 100:
         os.environ["CDP"] = "True"
         print os.environ["CDP"]                 # returns True
         self.child_pid = str(self.child_pid)
         cmd = 'kill -TERM' + " " + self.child_pid
         subprocess.Popen(cmd, shell=True)
def __init__(self):                              #Pseudo-Terminal in GTK window     
      self.v = vte.Terminal()                    #(displayed in a notebook)
      self.v.connect ("child-exited", lambda term: self.verif(self, a))
      self.v.connect('contents-changed', self.term)
      self.v.set_size(70,20)
      self.v.set_encoding("UTF-8")  
      self.v.set_cursor_blinks(False) 
      self.v.show()
      self.page.add(self.v)

The bash script is:

kill_jobs()
{
    pkill -TERM -P "$BASHPID"
    echo -e "$CDP"                                # returns False, should be True
    if [ "$CDP" == "True" ]; then
    echo -e "OPERATIONS ANNULEES"
    elif [ "$CDP" == "False" ]; then
    echo -e "OPERATIONS TERMINEES"
    fi
}

The problem is, $CDP = False so the message displayed is not good. What is the reason? Thanks

Guillaume
  • 2,752
  • 5
  • 27
  • 42

1 Answers1

0

After setting the environment via

os.environ["CDP"] = "True"

You can get this value in you bash only if you call the bash script via os.system(), os.popen() or os.fork() and os.execv().

so If you can add

os.system('/bin/bash script.sh')

You shall be able to use the value of CDP in bash script normally.

Please read os.putenv()

I guess os.environ and os.putenv() are closely related.

PradyJord
  • 2,136
  • 12
  • 19
  • @Guillaume +1 for Your question, it helped me understand the os.* even more. – PradyJord May 28 '14 at 11:20
  • I don't understand, because the shell script is already running when I try to cancel it – Guillaume May 28 '14 at 11:58
  • 1
    Please elaborate, can not guess what you meant. I am saying, you shall get `CDP` value in bash script only if you fire the script using `os.system()` or any of the methods I have mentioned above, If shell script is running and then you are running python program and thinking that `CDP` can be read as True then its not possible. Please kill both python and shell script now and make changes as I suggested and then look for the expected results. – PradyJord May 28 '14 at 12:02