简体   繁体   中英

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. I am a beginner in both python and linux, have been stuck on this problem the whole day. Would love to hear from you guys <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. 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. 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  '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?

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. It is an operator that invokes unix's handy little job control protocol, which allows the parallelization of complex programs! 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). The shell you are still in control of is called the foreground process .

now, what you have going on with gnome-terminal is a little more complicated. gnome-terminal is executing a bash terminal (which has a shell for each process you run inside it) in the GNOME environment. -e is the command you want to send to this terminal. 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. bash-c (commmand) is changing your wallpaper. Ok, cool. exec bash is probably any weird error thrown, if I had to guess. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM