2022年 11月 4日

Python 调用终端执行命令(gnome-terminal)

文章目录

    • 一、gnome-terminal 的使用
    • 二、Python 中的调用
    • 三、gnome-terminal manual
    • 参考

使用python 开启 Ubuntu 终端来执行命令,是使用 os 调用 gnome-terminal 命令。
这样的调用会出现各种各样的问题;首先应先确定命令本身没有问题,即在终端调用这个命令。

可以使用 gnome-terminal --help-all 查看命令帮助手册。


一、gnome-terminal 的使用

这里以实现 ls 命令为例,我需要调用 ls 命令,且执行完不自行关闭窗口。


正确写法:

gnome-terminal  -e 'bash -c "ls; exec bash" '
  • 1

以下写法错误、或没达到目标


1、执行完后会关闭终端

 gnome-terminal -- 'ls'
  • 1

the child process exited normally with status 0
  • 1

2、执行失败

gnome-terminal -- 'ls; exec bash'
  • 1

There was an error creating the child process for this terminal
Failed to execute child process "ls; exec bash" (No such file or directory)  
  • 1
  • 2

3、执行失败

gnome-terminal -- 'ls; bash'
  • 1

There was an error creating the child process for this terminal
Failed to execute child process  "ls; bash" (No such file or directory)  
  • 1
  • 2

更多

# 开启新tab 执行命令 --tab
gnome-terminal --tab  -e 'bash -c "ls; exec bash" '

# 设置标题为 hello gnome
gnome-terminal --tab -t "hello gnome"  -e 'bash -c "ls; exec bash" '

# 在 dir003 文件夹中执行命令
gnome-terminal --working-directory dir003 'bash -c "ls; exec bash" '
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

二、Python 中的调用

import os
os.system(''' gnome-terminal  -e 'bash -c "ls; exec bash" ' ''')
  • 1
  • 2

三、gnome-terminal manual

$ gnome-terminal --help-all
Usage:
  gnome-terminal [OPTION…] [-- COMMAND …]

Help Options:
  -h, --help                      Show help options
  --help-all                      Show all help options
  --help-gtk                      Show GTK+ Options
  --help-terminal                 Show terminal options
  --help-window-options           Show per-window options
  --help-terminal-options         Show per-terminal options

GTK+ Options
  --class=CLASS                   Program class as used by the window manager
  --name=NAME                     Program name as used by the window manager
  --gdk-debug=FLAGS               GDK debugging flags to set
  --gdk-no-debug=FLAGS            GDK debugging flags to unset
  --gtk-module=MODULES            Load additional GTK+ modules
  --g-fatal-warnings              Make all warnings fatal
  --gtk-debug=FLAGS               GTK+ debugging flags to set
  --gtk-no-debug=FLAGS            GTK+ debugging flags to unset

Options to open new windows or terminal tabs; more than one of these may be specified:
  --window                        Open a new window containing a tab with the default profile
  --tab                           Open a new tab in the last-opened window with the default profile

Window options; if used before the first --window or --tab argument, sets the default for all windows:
  --show-menubar                  Turn on the menubar
  --hide-menubar                  Turn off the menubar
  --maximize                      Maximize the window
  --full-screen                   Full-screen the window
  --geometry=GEOMETRY             Set the window size; for example: 80x24, or 80x24+200+200 (COLSxROWS+X+Y)
  --role=ROLE                     Set the window role
  --active                        Set the last specified tab as the active one in its window

Terminal options; if used before the first --window or --tab argument, sets the default for all terminals:
  -e, --command                   Execute the argument to this option inside the terminal
  --profile=PROFILE-NAME          Use the given profile instead of the default profile
  -t, --title=TITLE               Set the initial terminal title
  --working-directory=DIRNAME     Set the working directory
  --wait                          Wait until the child exits
  --fd=FD                         Forward file descriptor
  --zoom=ZOOM                     Set the terminal’s zoom factor (1.0 = normal size)

Application Options:
  --load-config=FILE              Load a terminal configuration file
  --preferences                   Show preferences window
  -p, --print-environment         Print environment variables to interact with the terminal
  -v, --verbose                   Increase diagnostic verbosity
  -q, --quiet                     Suppress output
  --display=DISPLAY               X display to use


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

参考

  • 通过Python 打开各系统(MAC, LINUX, WINDOWS)下的终端 (Terminal)
    https://blog.csdn.net/weixin_43988842/article/details/106156232