3

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?

EDIT:

Here is run.sh, which I run :

#!/bin/bash
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.:

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

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)
            co.wait()
        if self.speak == True:
            self.say(self.message)
        else:
            subprocess.Popen(["notify-send","SysSec", self.message])
4
  • Welcome to Ask Ubuntu! Posting the code you're using would help us to help you. May 1, 2013 at 9:31
  • @JorgeSuárezdeLis I updated the code. Please have a look. May 1, 2013 at 9:47
  • could you also post your python code? Try to remove unnecessary bits of code. May 1, 2013 at 9:52
  • @JorgeSuárezdeLis I have updated the rest of the code too. Thanks May 1, 2013 at 10:23

1 Answer 1

2

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:

#!/bin/bash
env > ~/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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .