0

I have a python app, which should run a bash script if a specific button is clicked. I put the app into menu, created the .desktop file, everything looks fine except that subprocess.call. I also tried it with os.system(), and several other solution, all of them working properly, if the app is started from terminal, but not, if it's started by its menu icon. Anyone know how to fix this? I want it to run without terminal window, from menu, but couldnt find a topic similar to mine

3 Answers 3

1

Okay, both answers work, because the problem was not with the missing terminal, but ME.

I left a code session in my app, which was originally intended to call the script execution, but it was called from another section, so there fixing the relative path

subprocess.call('./script')

to

subprocess.call('absolute_path_to_script') 
  • this made it immediately. I also tried to change to the absolute path, but the button never invoked that event so... my mistake, thanks for your time to try to help me
Sign up to request clarification or add additional context in comments.

3 Comments

I Upvoted you just for admitting your mistake. By code session did you mean that the working directory changed when started from GUI. This is normal. If you posted the traceback before, we could have told you what is happening at once. Have this in mind when asking on SO. Always post tries and results. Glad your app works!
I meant that the piece of code I was changing over and over for hours wasn't even executed, because nothing invoked it. Somewhere in time (months ago) I eventually changed my mind and put the subprocess.call to another function, and that function was invoked by the button_click event, which function I never changed. So whathever I tried to fix my code, didnt effect the code execution. :( But I learned my lesson :)
Woops, that was nasty in deed.
0

That is because GUI processes are detached from stdout/stderr and redirected to loging instead. Please explain what is your bash script doing, and, well, it is run without window, obviously. To check, make it write something to some file, and you will see that the script has been run. Or you refer that you don't like your process to have a console window, but your bash script should be run in one.

If so, make subprocess.call() call terminal first, with command to call bash script.

Please be more precise and explain clearly what you need so that we may help you.

If you really need a terminal to be started, I put together some code for you.

Problem with starting new terminal on Linux is that there are many different desktop environments.

Each of them usually carying its own terminal program.

Which one to use??? You may simply try until you succeed:

import subprocess
script = "~/full_path_to_your_bash_script.sh"
# Uncomment following line to ensure execution by bash and remove potential problems with permissions.
#script = "bash "+script
# Commands to call (add some more if you remember any):
commands = [
    ['gnome-terminal', '-x', script],
    # xterm is usually present:
    ['xterm', '-e', script],
    ['rxvt', '-e', script]
    ]
ok = 0
for command in commands:
    try:
        subprocess.call(command)
        ok = 1; break
    except OSError, e:
        if e.errno==2: continue # No such file or directory - skip non-existing terminal and try another
        raise # Some other OSError occurred
    except: raise
if not ok: raise RuntimeError, "No terminal available!"

3 Comments

Thanks, this seems good, I'll try it. My script runs some terminal commands for converting and editing image, and then sends it to printer. I' m new to python, i knew im missing a terminal for that script if i run app from menu, but i had no idea how to get one during runtime.
If these commands doesn't use stdout/stderr for nothing specific but some informative text and they do not use anything from stdin then running the script without terminal/console should work.
@kov%c3%a1cs-gabika-ella: I edited my A. and added some code that might help.
0

try to add shell=True in call statement. subprocess doc

i.e.

subprocess.call("exit 1", shell=True)

1 Comment

I am not sure that this would help. shell=True means that the command will be processed first through shell, and that shell will pass it to OS. If False shell is not used. Command is passed to OS directly. Although shell=True might force shell to start, I don't think that this will help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.