[英]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.