Running gnome-terminal with arguments

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

I tried to do:

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

and it works the way I want, a new window opens.

But how do I pass arguments? For example, when the terminal starts up, I want it to say start ls. But this:

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

This works again, but the command lsdoes not work: an empty terminal window starts.

So my question is: how to launch a terminal window with the specified command so that the command starts when the window opens.

PS: I only target Linux.

+3
source share
3 answers
$ gnome-terminal --help-all

 ...

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

 ...

, , , .

+5
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>
+5

this is the system that I use to run the gnome terminal from notepad ++ to WINE,

1: notepad ++ command to run

#!/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 the sub script that I use to run bash after completing my program (usually this is python)

#!/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
0
source

Source: https://habr.com/ru/post/1784242/


All Articles