简体   繁体   中英

executing (service ssh start) from shell script works, but when I call the script from python, it doesnt work

I have a shell script that at some point starts a docker container, and then starts ssh on it

out=$(docker start $CONTAINER)


if ! [[ $? -eq 0 ]]; then
      echo $out
      exit 1
   else
      echo "docker started successfully "
      out=$(docker exec -ti $CONTAINER service ssh start)
      if ! [[ $out =~ "[ OK ]" ]]; then
         echo "SSH failed!"
         exit 1

    else
         echo "SSH running successfully"
      fi
   fi

This works just fine. However when I call this shell script from inside a python script (via subprocess), the python script stops responding as soon as it gets to the “docker exec" part, and I immediately get a SIGTERM: fish: Job 2, 'python3 start.py &' terminated by signal SIGTERM (Polite quit request)

def runScript(self)
    self.p = subprocess.Popen(["./script.sh"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, bufsize=1, universal_newlines=True)
    while self.p.poll() is None:
            msg = self.p.stdout.readline().strip()
            if msg:
                print(msg)

self.job = ExecutionThread(target=self.runScript, daemon=True, kwargs=dict(tmpTxt =self.tmpCommandTxt))

I dont want to use the docker lib from python directly for some other reasons, I want it to happen from within the shell script.

I also tried removing the "docker start" part for the shell script (the script it self is huge). In this case it works fine. So the problem is clearly with the docker part of the script.

The problem was with the "i" argument being passed to docker exec. I dont have a 100% correct explanation, but apparently trying to get an interactive session from within a background python created subprocess goes bad:)

so changing the command to the following, works fine.

out=$(docker exec -t $CONTAINER service ssh start)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM