15

I´m trying to count how many shell or terminal windows are currently running.

I tried ps a|grep bash; ps a|grep tty;

But it can be inaccurate.

2
  • What's your definition of terminal window? Do a gnome-terminal tab, virtual terminal, person logging in over ssh, screen/tmux pane, expect emulated user session, xterm not running a shell, etc count? Commented May 29, 2014 at 18:40
  • 1
    Terminal window opened with "ctrl-alt-t". I solved it by counting how many "pts" are in >who command who|grep "pts"|wc -l
    – Martincho
    Commented May 29, 2014 at 18:57

5 Answers 5

10
ls /dev/pts/  | wc -l

Use the above command to count the terminal windows open.

3
  • Tried that. It shows 23. And I have only one terminal/bash window open.
    – Martincho
    Commented May 29, 2014 at 18:18
  • @user68957 Are you on a shared computer with multiple users by any chance? Commented May 29, 2014 at 18:26
  • One home computer, one user at the time. The trick would be to count how many terminal windows are open, or terminal processes. If I do a >who, it list two users me :0 yyyy-mm-dd hh-mm (:0) me pts/0 yyyy-mm-dd hh-mm (:0) If I open another terminal window, with >who it shows: me :0 yyyy-mm-dd hh-mm (:0) me pts/0 yyyy-mm-dd hh-mm (:0) me pts/1 yyyy-mm-dd hh-mm (:0) Should I just grep "pts/"?
    – Martincho
    Commented May 29, 2014 at 18:44
4

To list them:

ps aux | awk '{print $7}' | grep -v "?"

To count them:

ps aux | awk '{print $7}' | grep -v "?" | wc -l

You will need to subtract 1 from that number as it includes the top TTY header.

This all depends if you are wanting to count how many sub shells are running or if you are wanting to count how many terminal windows are open.

To count just the terminal windows you would need to use:

ls /dev/pts/ | wc -l (stated in a previous answer)

For example:

On my system there are currently six tty's available. I also have one terminal open pts/0 that has 4 processes running in the fg or bg.

root      4565  0.0  0.0   4060   576 tty1     Ss+  May01   0:00 /sbin/mingetty /dev/tty1
root      4567  0.0  0.0   4060   572 tty2     Ss+  May01   0:00 /sbin/mingetty /dev/tty2
root      4569  0.0  0.0   4060   568 tty3     Ss+  May01   0:00 /sbin/mingetty /dev/tty3
root      4571  0.0  0.0   4060   576 tty4     Ss+  May01   0:00 /sbin/mingetty /dev/tty4
root      4573  0.0  0.0   4060   576 tty5     Ss+  May01   0:00 /sbin/mingetty /dev/tty5
root      4575  0.0  0.0   4060   572 tty6     Ss+  May01   0:00 /sbin/mingetty /dev/tty6

me  17482  0.0  0.0 110236  1136      pts/0    R+   11:36   0:00 ps aux
root     20374  0.0  0.0 108336  1816 pts/0    Ss   May23   0:00 -bash
root     20953  0.0  0.1 161436  1960 pts/0    S    May23   0:00 su - me
me  20954  0.0  0.1 108524  1984      pts/0    S    May23   0:00 -bash

If you want to take away the background child processes then just pipe to uniq:

ps aux | awk '{print $7}' | grep -v "?" | uniq | wc -l

You still have to subtract 1 for the header title of TTY, but you can further improve this by taking the tty's out altogether as it appears you don't care about those anyways.

ps aux | awk '{print $7}' | grep -v "?" | grep -vi "tty*" | uniq

That will give you an accurate count.

EDIT

Thinking about it more 'ps -a' would work better and you can leave out the first grep.

ps a | awk '{print $2}' | grep -vi "tty*" | uniq | wc -l
8
  • grep and awk is redundant, use awk '/pattern/; and this doesn't work on my machine - it reports 19 when I have 1.
    – jasonwryan
    Commented May 29, 2014 at 18:21
  • I am a bit confused. Right now, I´m on Ubuntu 14. There is only one terminal/bash window open. With the abobe commands, I get 23 or 13, when I should get 1, or 2 in case I open another terminal with ctrl-alt-t
    – Martincho
    Commented May 29, 2014 at 18:21
  • That first command counts children processes running in shells in the background and not just open terminal sessions
    – Jeight
    Commented May 29, 2014 at 18:31
  • @jasonwryan - It works just fine on mine. Using grep with awk is person preference. If someone is more comfortable doing the whole thing in awk then that's fine.
    – Jeight
    Commented May 29, 2014 at 18:34
  • "It works just fine on mine" hardly substantiates your answer... Perhaps you could include some detail of what "yours" is so people are aware that it only applies to that system.
    – jasonwryan
    Commented May 29, 2014 at 20:50
0

On macos you can use:

cnt=$(w -h | grep "^$(whoami) *s[^ ]* *-"|wc -l)
echo "Your current terminal sessions: $cnt"

Found here: https://stackoverflow.com/a/6180547/6320039

-1

You could do $ pgrep bash

enter image description here

Also you can do $ W

enter image description here

-1

You can add the \l option in your PS1 variable, example:

export PS1="terminal #\l \W \$ "

That will count your terminals starting from zero.

More about it in the GNU website.

Hope this helps.

2
  • 1
    The doco that you point to tells you that it does not count anything.
    – JdeBP
    Commented Aug 29, 2020 at 19:17
  • "The basename of the shell’s terminal device name." That's what it says, I tried it myself and (in practice) it shows the terminal number starting from 0, and if you are inside a tty (no gui) it will tell you which tty you are in.
    – Merazi
    Commented Aug 30, 2020 at 18:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .