I have a shell command that executes a command and sends it through pipe to a python file. The python file does various checks on it and then starts processes with parameters using subprocess.Popen. But I see that it only works when I run the entire thing in a shell. Is there another way for me to execute it other than shell. If I just run it without any shell or use & to keep it at background, the processes started by python are named defunct in the ps -e table. Can someone help?


Here is run.sh, which I run :

padsp julius -input mic -C $HOME/project/julius-grammer/julian.jconf | python -u $HOME/project/pythonControls/getcommand.py

However not all subprocess.popen fail to run. For example, if I use festival program, it runs. However xbacklight doesn't.

this is getcommand.py:

I removed the unnecessary part, and this the one that is necessary, com is string, I know this selection is going on fine.:

    userin = Data(["xbacklight", "-set", "100"],"Maximum brightness")
if(com == "HALF BRIGHTNESS"):
    userin = Data(["/usr/bin/xbacklight", "-set", "50"],"Partial brightness")
if(com == "ZERO BRIGHTNESS"):
    userin = Data(["/usr/bin/xbacklight","-set", "0"],"Minimal brightness")

Now coming to the objects, this is how the class goes:

class Data:
    def __init__(self, com="", msg="", sp="False"):
        self.command = com
        self.message = msg
        self.speak = sp
    def interact(self):
        if self.command != "":
            co = subprocess.Popen(self.command,stdout=subprocess.PIPE)
        if self.speak == True:
            subprocess.Popen(["notify-send","SysSec", self.message])
  • Welcome to Ask Ubuntu! Posting the code you're using would help us to help you. – Jorge Suárez de Lis May 1 '13 at 9:31
  • @JorgeSuárezdeLis I updated the code. Please have a look. – user1652263 May 1 '13 at 9:47
  • could you also post your python code? Try to remove unnecessary bits of code. – Jorge Suárez de Lis May 1 '13 at 9:52
  • @JorgeSuárezdeLis I have updated the rest of the code too. Thanks – user1652263 May 1 '13 at 10:23

When you run a script without a console, you're doing it with the graphical session environment.

Create a file named testenv.sh with these lines:

enc > ~/testenv.txt

Put the file on the same directory as your script. Run it from the graphical session. Open the file, save it elsewhere. Then run the file from a terminal and compare the output.

Chances are that the problem here is the PWD. You're relying on the script being run on the same directory where you are, but all the scripts run with the graphical session environment, are run with the PWD set at your home directory.

It might also be a problem with your script lacking a shebang.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.