如何使用bash脚本关闭所有终端,该脚本在每个终端中有效地按Cntrl + Shift + Q [英] How to close all terminals with a bash script that effectively presses Cntrl+Shift+Q in each terminal

查看:97
本文介绍了如何使用bash脚本关闭所有终端,该脚本在每个终端中有效地按Cntrl + Shift + Q的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常有很多终端处于打开状态,其中一些正在运行重要的进程(例如服务器),而另一些没有运行并且可以关闭的终端.如果您在其中按下 Cntrl + Shift + Q ,则重要"提示会提示您进行确认,如下所示.

我想有一个(bash)脚本,它关闭所有终端,但以上面显示的模式保留重要"的终端.来自> https://askubuntu.com/questions/608914/closing-all-instances-of-terminal-at-一次干净,我获得了以下脚本:

 #!/bin/bashxdotool搜索--class终端" |同时读取ID做xdotool windowactivate"$ id"&>/dev/nullxdotool键Ctrl + Shift + q睡0.2完毕 

但是,运行脚本后,我发现某些不重要"的终端仍然保持打开状态.上面的脚本中是否可能有错误?

解决方案

另一种方法是杀死所有空闲的 bash 进程,即没有子进程的进程.这也应该关闭其父终端窗口.它的运行速度比 xdotool + sleep 0.2 方法快得多.

这是我如何查看我的 bash 进程的进程树的方法:

  pgrep bash |xargs -n1 pstree -p -c 

输出:

  bash(1470)───startx(1546)───xinit(1568)─┬─Xorg(1569)─┬─{Xorg}(1570)│├─{Xorg}(1571)│└─{Xorg}(1572)└─dwm(1575)扑(1582)扑(4004)重击(4125)bash(28105)───nvim(17279)─┬─R(17956)─┬─{R}(17958)│├─{R}(17959)... 

这里的第一个和最后一个 具有子进程,我不想杀死它们.中间的三个可以安全地被杀死,这也将关闭其父终端窗口.首先,我将通过用"-"过滤掉所有行来仅选择那些行:

  pgrep bash |xargs -n1 pstree -p -c |grep -v \- 

输出:

  bash(1582)扑(4004)重击(4125) 

接下来,我将再次使用 grep 仅包括进程ID号:

  pgrep bash |xargs -n1 pstree -p -c |grep -v \-|grep -o'[0-9] \ +' 

输出:

  158240044125 

最后,杀死他们:

  pgrep bash |xargs -n1 pstree -p -c |grep -v \-|grep -o'[0-9] \ +'|xargs杀死 

I often have many terminals open, some of which are running important processes (e.g. servers) and others which are not running anything and can be closed. The 'important' ones bring up prompts for confirmation if you press Cntrl+Shift+Q in them, like the one shown below.

I would like to have a (bash) script which closes all terminals but leaves the 'important' ones in the mode shown above. From https://askubuntu.com/questions/608914/closing-all-instances-of-terminal-at-once-cleanly, I obtained the following script:

#!/bin/bash

xdotool search --class "terminal" | while read id
do
      xdotool windowactivate "$id" &>/dev/null
      xdotool key ctrl+shift+q
      sleep 0.2
done 

However, I found after running the script that some 'unimportant' terminals still remained open. Is there perhaps a bug in the script above?

解决方案

another way to do this is to kill any idle bash processes, that is, those with no subprocesses. this should also close their parent terminal windows. it will run much faster than the xdotool + sleep 0.2 method.

here is how i can see process trees for my bash processes:

pgrep bash | xargs -n1 pstree -p -c

output:

bash(1470)───startx(1546)───xinit(1568)─┬─Xorg(1569)─┬─{Xorg}(1570)
                                        │            ├─{Xorg}(1571)
                                        │            └─{Xorg}(1572)
                                        └─dwm(1575)
bash(1582)
bash(4004)
bash(4125)
bash(28105)───nvim(17279)─┬─R(17956)─┬─{R}(17958)
                          │          ├─{R}(17959)
...

the first and last bashs here have subprocesses and i don't want to kill them. the middle three can safely be killed, which will also close their parent terminal windows. first, i'll select only those by filtering out any lines with a "-":

pgrep bash | xargs -n1 pstree -p -c | grep -v \-

output:

bash(1582)
bash(4004)
bash(4125)

next, i'll use grep again to include only the process id numbers:

pgrep bash | xargs -n1 pstree -p -c | grep -v \- | grep -o '[0-9]\+'

output:

1582
4004
4125

finally, kill them:

pgrep bash | xargs -n1 pstree -p -c | grep -v \- | grep -o '[0-9]\+' | xargs kill

这篇关于如何使用bash脚本关闭所有终端,该脚本在每个终端中有效地按Cntrl + Shift + Q的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆