user225312
user225312

Reputation: 131807

Starting gnome-terminal with arguments

Using Python , I would like to start a process in a new terminal window, because so as to show the user what is happening and since there are more than one processes involved.

I tried doing:

>>> import subprocess
>>> subprocess.Popen(['gnome-terminal'])
<subprocess.Popen object at 0xb76a49ac>

and this works as I want, a new window is opened.

But how do I pass arguments to this? Like, when the terminal starts, I want it to say, run ls. But this:

>>> subprocess.Popen(['gnome-terminal', 'ls'])
<subprocess.Popen object at 0xb76a706c>

This again works, but the ls command doesn't: a blank terminal window starts.

So my question is, how do I start the terminal window with a command specified, so that the command runs when the window opens.

PS: I am targetting only Linux.

Upvotes: 6

Views: 10339

Answers (3)

admalledd
admalledd

Reputation: 448

this is the system that I use to launch a gnome-terminal from notepad++ in WINE,

1:notepad++ command to launch

#!/usr/bin/python
#this program takes three inputs:::
#$1 is the directory to change to (in case we have path sensitive programs)
#$2 is the linux program to run
#$3+ is the command line arguments to pass the program
#
#after changing directory, it launches a gnome terminal for the new spawned linux program
#so that your windows program does not eat all the stdin and stdout (grr notepad++)

import sys

import os
import subprocess as sp

dir = sys.argv[1]
dir = sp.Popen(['winepath','-u',dir], stdin=sp.PIPE, stdout=sp.PIPE).stdout.read()[:-1]

os.chdir(os.path.normpath(os.path.realpath(dir)))
print os.getcwd()

print "running '%s'"%sys.argv[2]
cmd=['gnome-terminal','-x','run_linux_program_sub']
for arg in sys.argv[2:]:
    cmd.append(os.path.normpath(os.path.realpath(sp.Popen(['winepath','-u',arg], stdin=sp.PIPE, stdout=sp.PIPE).stdout.read()[:-1])))
print cmd
p = sp.Popen(cmd, stdin=sp.PIPE, stdout=sp.PIPE)

2: run sub script, which I use to run bash after my program quits (python in this case normally)

#!/bin/sh
#$1 is program to run, $2 is argument to pass
#afterwards, run bash giving me time to read terminal, or do other things
$1 "$2"
echo "-----------------------------------------------"
bash

Upvotes: 0

unutbu
unutbu

Reputation: 880767

In [5]: import subprocess

In [6]: import shlex

In [7]: subprocess.Popen(shlex.split('gnome-terminal -x bash -c "ls; read -n1"'))
Out[7]: <subprocess.Popen object at 0x9480a2c>

Upvotes: 5

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799380

$ gnome-terminal --help-all

 ...

  -e, --command                   Execute the argument to this option inside the terminal

 ...

If you want the window to stay open then you'll need to run a shell or command that keeps it open afterwards.

Upvotes: 6

Related Questions