Skip to content

Commit ffc9278

Browse files
adds count_active_terminals method to retrieve unique active terminal sessions
1 parent 6a4ce43 commit ffc9278

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

src/models/process_group.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import subprocess
12
from models.process import Process
23

34

@@ -323,6 +324,30 @@ def create_fake_processes(self) -> None:
323324
)
324325
)
325326

327+
def count_active_terminals():
328+
try:
329+
# Run 'who' to get active login sessions
330+
result = subprocess.run(
331+
["who"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
332+
)
333+
334+
if result.returncode != 0:
335+
raise RuntimeError(f"Error running 'who': {result.stderr}")
336+
337+
lines = result.stdout.strip().split("\n")
338+
339+
# Extract terminals from the output (2nd column)
340+
terminals = [line.split()[1] for line in lines if line]
341+
342+
# Remove duplicates, in case multiple sessions use the same terminal
343+
unique_terminals = set(terminals)
344+
345+
return len(unique_terminals)
346+
347+
except Exception as e:
348+
print(f"Error checking active terminals: {e}")
349+
return 0
350+
326351
def add_process(self, process: Process) -> None:
327352
"""Add a new process to the process group."""
328353
if process.pid in self.processes:

0 commit comments

Comments
 (0)