Yes, it is. A picture worth a thousand words:
So, you have to redirect the output of your command using >
operator to /dev/pts/#
. You can find #
using who
or w
command. If tou want to redirect and the errors, use:
<command> >& /dev/pts/#
Even the "friendliest" kill- command will close the terminal without asking. Also man gnome-terminal
does not give any solution to close the window like in the GUI.
You can however make a script raise (all) gnome-terminal
windows and simulate Ctrl+Shift+Q.
A complexity is that this will not work when the windows are spread over different workspaces. The script below therefore looks up the gnome-terminal
windows on the current workspace and takes care of them as explained above.
The script
#!/usr/bin/env python3
import subprocess
import time
def get_res():
# get resolution
xr = subprocess.check_output(["xrandr"]).decode("utf-8").split()
pos = xr.index("current")
return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]
try:
pid = subprocess.check_output(["pidof", "gnome-terminal"]).decode("utf-8").strip()
except:
pass
else:
res = get_res()
ws = subprocess.check_output(["wmctrl", "-lpG"]).decode("utf-8").splitlines()
for t in [w for w in ws if pid in w]:
window = t.split()
if all([0 < int(window[3]) < res[0], 0 < int(window[4]) < res[1]]) :
w_id = window[0]
subprocess.Popen(["wmctrl", "-ia", w_id])
subprocess.call(["xdotool", "key", "Ctrl+Shift+Q"])
time.sleep(0.2)
How to use
The script needs both wmctrl
and xdotool
sudo apt-get install xdotool
sudo apt-get install wmctrl
Copy the script into an empty file, save it as close_allterminals.py
.
Test-run it by the command:
python3 /path/to/close_allterminals.py
Example: four gnome-terminal
windows opened, in the top-left one is a process running:
After running the command, three are closed automatically, the one with the running process gets a prompt:
If all works as you like, add it to a shortcut key combination:
choose System Settings > Keyboard > Shortcuts > Custom Shortcuts. Click the "+" and add the command:
python3 /path/to/close_allterminals.py
Edit
The version below also takes care of gnome-terminal
windows on other workspace: all windows are moved to the current workspace before they are closed in a safe way.
An example:
I have in total six gnome-terminal
windows open on four different workspaces, many of them have processes running in it:
If I run the script, all gnome-terminal
windows are orderly moved to the current workspace and raised. Idle windows are closed automatically, the ones with a running process are prompted:
The script
Set it up like the first version.
#!/usr/bin/env python3
import subprocess
import time
def get_res():
# get resolution
xr = subprocess.check_output(["xrandr"]).decode("utf-8").split()
pos = xr.index("current")
return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )]
try:
pid = subprocess.check_output(["pidof", "gnome-terminal"]).decode("utf-8").strip()
except:
pass
else:
res = get_res()
ws = subprocess.check_output(["wmctrl", "-lpG"]).decode("utf-8").splitlines()
matches = [t.split() for t in [w for w in ws if pid in w]]
pos = 100
for match in matches:
w_id = match[0]
subprocess.call(["xdotool", "windowmove", "--sync", match[0], str(pos), str(pos/2) ])
subprocess.call(["wmctrl", "-ia", w_id])
subprocess.call(["xdotool", "key", "Ctrl+Shift+Q"])
pos = pos+100
Best Answer
You are killing the program
SeondApp
, but you are not killing the terminal it is running in. The two are separate things. For example, this is the process tree of runninggedit
in a terminal:Ignore the
systemd
, that's theinit
process, everything running on your machine is a child ofsystemd
. Then, what you see there is thatgnome-terminal
has launchedbash
which then runsgedit
. If you now killgedit
, that won't affect its parents. However, if you kill one of the parents, that will also kill the child.Normally, what you would do is to use
$!
, a special variable that holds the PID of the last process launched to the background. Unfortunately, that doesn't withgnome-terminal
which seems to have some sort of complicated launching procedure:As you can see above, gnome-terminal seem to re-launch itself after launching and uses a different PID. No idea why, but another good reason to use a different terminal.
So, since the standard approach won't work, we need a workaround. What you can do is use
kill -$PID
which will kil all processes in the process group (fromman kill
):Putting all this together, here's a working version of your script:
Note that I also remove the
[ ]
around! pgrep
since that was wrong syntax.I don't see why you are launching terminals at all though. Here's the same idea, without terminals:
Finally, this feels like a strange way of doing things. You might want to ask a new question, explain what you are trying to do and why and we can see if we can find a simpler approach for whatever it is you need.