How to run a shell script without terminal

5,151

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.

Share:
5,151

Related videos on Youtube

user1652263
Author by

user1652263

Updated on September 18, 2022

Comments

  • user1652263
    user1652263 over 1 year

    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])
    
    • user1652263
      user1652263 almost 11 years
      @JorgeSuárezdeLis I updated the code. Please have a look.
    • user1652263
      user1652263 almost 11 years
      @JorgeSuárezdeLis I have updated the rest of the code too. Thanks