简体   繁体   English

关闭终端窗口后如何运行特定的bash shell脚本?

[英]How to run a particular bash shell script after closing a terminal window?

By inserting a script in .bashrc file, I am able to run that script when new terminal window is created. 通过在.bashrc文件中插入脚本,可以在创建新的终端窗口时运行该脚本。 Is there any similar method to run a script after closing a terminal? 关闭终端后,是否有任何类似的方法来运行脚本?

All I want is to keep the count of number of terminal windows that are currently open. 我只想保留当前打开的终端窗口数。 If you have any other method to do it, please tell. 如果您有其他方法可以执行此操作,请告知。

Thank you. 谢谢。

You should just be able to grab a count of PTY's on the system: ls /dev/pts/ | wc -l 您应该只能够在系统上获取PTY的数量: ls /dev/pts/ | wc -l ls /dev/pts/ | wc -l These should only be active in that directory if they are in use. ls /dev/pts/ | wc -l这些仅在使用中时才在该目录中处于活动状态。

You can use this line: 您可以使用以下行:

ps a | awk '{print $2}' | grep -vi "tty*" | uniq | wc -l

first get all processes with ps , pipe it to awk to get just the second column, use grep to ignore the "tty*", omit multiple lines with uniq and then count the lines with wc -l . 首先使用ps获取所有进程,将其传送到awk以获得第二列,使用grep忽略“ tty *”,使用uniq省略多行,然后使用wc -l计数行。

This should do the trick. 这应该可以解决问题。

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

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