Reputation: 367
In Linux, each terminal is associated with only one session (a session has one or more process groups, and a process group has one or more processes).
Is there some function (or a command) that takes a tty device file (for example: /dev/tty1
or /dev/pts/0
) and returns the session id associated with this tty/terminal?
Upvotes: 3
Views: 13384
Reputation: 1
By Mohit BATRA =
Each time you open a terminal a new session gets started and a unique session Id is assigned to it
if We need to find the session id associated with each terminal then it can be possible by = ps
command , and
By = tty
command we can find the terminal type or number and to get the session I'd associated with it we need to open the terminal two more time and check its terminal number with = tty then run
ps -exo sess,tty | grep pts/1
Here pts/1 is my terminal number which I have get by running the command = tty on the terminal and this is different in your case just note your and paste after grep and you will get the session id of that session Good luck.
Upvotes: 0
Reputation: 8573
Perform the following steps:
stat
the TTY you want to check. In particular, find out the major/minor device id it is using. Combine them to a single number using the formula major*256+minor
(or just take the raw number from stat
)
Open /proc/
and scan all directories whose name is only digits. The directory's name is the pid
of a process in the system.
For each such directory, open /proc/pid/stat
, and parse the file into fields separated by a space (except the second field, which has brackets around it). The 7th field will be the TTY device's major/minor. Scan until you find one that matches the TTY for which you are looking.
The 6th field in that file is the sid
for the process (the number you're looking for). The 8th field is the TTY's pgrp
.
File structure detailed here.
Upvotes: 3