简体   繁体   English

如何从第一个终端打开第二个终端并在其中运行命令? (没有子进程)

[英]How to open a 2nd terminal from 1st one and run command in it? (without it being child process)

I am trying to write a script in python which should change my desktop wallpaper on my raspberry pi. 我试图用python编写脚本,这应该更改我的树莓派上的桌面墙纸。 I am a beginner in both python and linux, have been stuck on this problem the whole day. 我是python和linux的初学者,整天都被卡在这个问题上。 Would love to hear from you guys <3 很想听听你们<3

This is the terminal command which changes my desktop wallpaper: 这是更改我的桌面墙纸的终端命令:

pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg

Concerning only the linux terminal syntax: i would like to open a second terminal and run a command in it, all initiated from the first terminal. 仅关于linux终端语法:我想打开第二个终端并在其中运行命令,所有命令均从第一个终端启动。 If i type into my first terminal: 如果我在第一个终端输入:

pi@raspberrypi:~ $ lxterminal &

it opens a new terminal window which stays open, and is not a child process right? 它会打开一个新的终端窗口,该窗口保持打开状态,这不是子进程吗? In this 2nd terminal my change wallpaper command works. 在此第二终端中,我的change wallpaper命令起作用。 The following command does not work and if i put a "&" next to gnome-terminal it opens a new terminal but does not execute the command that was specified with -e and gives me an error. 以下命令不起作用,如果我在gnome-terminal旁边放置“&”,它将打开一个新终端,但不执行用-e指定的命令,并给我一个错误。

gnome-terminal -e  'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg; exec bash\"'

How do you open a new terminal with a command passed with -e which is also not a child process? 如何使用通过-e传递的命令(也不是子进程)打开新终端?

I know you are new so I want to introduce some concepts to you before I can answer your question. 我知道您是新手,因此在向您提出问题之前,我想向您介绍一些概念。

The "&" operator in shell/unix is not meant to open a new terminal. shell / unix中的“&”运算符并不意味着要打开新的终端。 It is an operator that invokes unix's handy little job control protocol, which allows the parallelization of complex programs! 它是一种操作员,可调用unix方便的小作业控制协议,该协议允许并行化复杂程序! It's awesome. 这很棒。 It makes that command a background process , which basically means it starts a new shell (or "terminal" in the language of your OP) which runs that process and leaves you in control of your current shell (or terminal). 它使该命令成为后台进程 ,这基本上意味着它将启动一个新的Shell(或用OP语言表示的“终端”),该外壳程序将运行该进程并使您可以控制当前的Shell(或终端)。 The shell you are still in control of is called the foreground process . 您仍在控制中的shell称为前台进程

now, what you have going on with gnome-terminal is a little more complicated. 现在,您对gnome-terminal进行的操作有些复杂。 gnome-terminal is executing a bash terminal (which has a shell for each process you run inside it) in the GNOME environment. gnome-terminal在GNOME环境中执行一个bash终端(该终端具有在其中运行的每个进程的外壳)。 -e is the command you want to send to this terminal. -e是您要发送到此终端的命令。 So, you put the ampersand (&) at the end of that command if that is the command you desire to send to the background. 因此,如果要发送到后台的命令是“&”号,则将其放在该命令的末尾。

Now, let's look at the command you want to run: 现在,让我们看一下您要运行的命令:

gnome-terminal -e  'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg; exec bash\"'

-e indicates the command you want to run in the new terminal. -e表示要在新终端中运行的命令。 bash-c (commmand) is changing your wallpaper. bash-c(命令)正在更改您的墙纸。 Ok, cool. 好的。 exec bash is probably any weird error thrown, if I had to guess. 如果我不得不猜测,exec bash可能会引发任何奇怪的错误。 But that line should perform nothing at all. 但是那条线根本不执行任何操作。

What it sounds like to me is you don't really need to send anything to the background. 在我看来,您实际上不需要将任何内容发送到后台。

gnome-terminal -e  'bash -c \"pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/wallpaper.jpg

should change your wallpaper. 应该改变你的墙纸。 But, to answer the question completely, just place the & AFTER whichever command you wish to send in the background. 但是,要完全回答问题,只需将&AFTER放置在您希望发送的任何命令的背景中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:MQTT,第二个进程不能干扰第一个进程 - Python: MQTT, 2nd process cannot interfere the 1st process 如何将列表列表中与第一个列表中的单词匹配的单词替换为与第二个列表中的第一个单词具有相同位置的单词? - How can I replace a word in my list of lists that matches a word from 1st list into a word that has the same position as the 1st one in the 2nd list? 如何使用 remove() 删除列表中第 2 次出现的项目而不删除 Python 中的第一次出现 - How to remove 2nd occurrence of an item in a list using remove() without removing 1st occurrence in Python 如何在 python 类中创建具有一个参数的第一个方法和具有两个参数的第二个方法 - How to create a 1st Method with one Arguments and 2nd Method with two arguments in a python class Python:将变量从第一个脚本传递到第二个脚本,并将不同的变量从第二个脚本传递到第一个脚本 - Python: Passing variable from 1st script to 2nd script and passing different variable from 2nd script to 1st script 如何使用 2nd Tkinter window 更改 1st 中的图像? - How to use 2nd Tkinter window to change image in 1st? 如何根据字符串的 id 从给定的文本文件中提取字符串的第一个、第二个和最后一个实例? - How to extract 1st, 2nd and last instance of a string from a given text file based on their ids? 如何使用函数从字符串中提取第一个、第二个和最后一个单词? - How to extract 1st, 2nd and last words from the string using functions? 从列表列表中删除第一个元素,然后是第一个和第二个元素[保留] - Removing the 1st element then 1st and 2nd element from a list of lists [on hold] 如何为具有相同第一行值的每个第二行值快速处理 2xN 列表/nparray? - How to fast process 2xN list/nparray for each 2nd row value which has the same 1st row value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM