简体   繁体   中英

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. 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 ls /dev/pts/ | wc -l These should only be active in that directory if they are in use.

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 .

This should do the trick.

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