3

We have a system where we use different system accounts to control different environments. My standard flow looks like this:

$ ssh server.example.com
robin@server$ sudo su - env1-controller
env1-controller@server$ deploy

However, there may be a number of people doing the same thing, switching to the env1-controller user. And there's potential for us stepping on each other's toes by trying to modify the environments at the same time.

Is there any way to check if anyone else is currently using the env1-controller user?

2
  • 2
    If everyone uses sudo you could consider using something along the lines ps -ax | grep sudo to see who is currently using sudo or env1-controller and by running who you could get an idea on who is actually running it. – Seth Nov 23 '16 at 9:30
  • thanks @Seth, ps -ax | grep "sudo su - env1-controller" works perfectly, to get the PID. Could you outline how I could use who to then check the user that is running it? – Robin Winslow Nov 23 '16 at 9:51
2

If everyone uses sudo you could consider using something along the lines ps -ax | grep sudo to see who is currently using sudo or env1-controller and by running who you could get an idea on who is actually running it.

As an example on my machine from my machine the output of ps -ax | grep sudo would like this:

24324 pts/0    S      0:00 sudo su <user>

The first entry is the PID, the second the TTY, third time and last command. If you use who you get a list of currently logged in users and which TTY they're using. It could look like this:

<user>     pts/0        Nov 23 10:24 (<IP>)
<user>     pts/2        Nov 23 10:25 (<IP>)

So through that combination you know that the sudo is connect to whatever user is using pts/0. This would be the manual way to check.

My guess would be that it's possible to combine that information but my bash skills aren't good enough to just post the answer on how to do that.

The final solution used by the original author of the question was/is:

I actually ended up using grep to extract the exact TTY numbers, ps -aux | grep -v grep | grep "sudo su - whoami" | grep -E -o 'pts/[0-9]+' | grep -E -o '[0-9]+', and then using ls -l /dev/pts/${id} to determine who owned the TTY.

What this does is:

  • Use ps -aux to get a list of all running processes including some additional information.
  • grep all lines that don't contain grep in order to exclude the grep from the output.
  • grep the line for all processes that contain sudo.
  • grep the text pts/<number> from those lines.
  • grep just the number.
  • Use ls to check who's the owner of that TTY.

Using that information you could try to do this (if you wanted to use who):

who | grep $(ps -ax | grep -v grep | grep "sudo su - whoami" | grep -E -o 'pts/[0-9]+')

But it would only work as long as there is a single result because the output from $() would have multiple lines. As you can see I'm skipping the u from the ps as the added information isn't needed for the basic script to work.

3
  • 1
    I actually ended up using grep to extract the exact TTY numbers, ps -aux | grep -v grep | grep "sudo su - whoami" | grep -E -o 'pts/[0-9]+' | grep -E -o '[0-9]+', and then using ls -l /dev/pts/${id} to determine who owned the TTY. – Robin Winslow Nov 23 '16 at 11:55
  • Thanks for adding it. I incorporated an explanation of your solution into the answer if you don't mind. – Seth Nov 23 '16 at 14:54
  • 1
    Ah yeah, good stuff. The reason I am inspecting the contents of /dev/pts/ rather than looking at the output of who is that sometimes the attached TTY isn't actually a direct login session - sometimes it's a tmux or screen session. In this case, you won't find the TTY in the output of who, but there will still be a file in /dev/pts belonging to the user who owns the session. – Robin Winslow Nov 23 '16 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.