Find out if there is a local user logged in when logging in over ssh

7,125

Solution 1

Use w

From w man page:

Show who is logged on and what they are doing.

Output example:

$ w
09:15:10 up 43 min,  2 users,  load average: 0.74, 0.38, 0.24

USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU  WHAT

kucing   tty7     :0               08:32   43:15m 57.73s  0.18s x-session-manager
kucing   pts/0    :0.0             08:48    0.00s  0.24s  0.00s w´

Solution 2

Is the finger command installed, or if not, could it be? This should give a listing of all users who are logged in and where from (i.e. another machine, or directly onto the machine), and should also tell you how long that user has been idle.

See the ubuntu finger manpage for more information.

Solution 3

last

it looks through /var/log/wtmp and displays a log of the last users logged on, including those currently logged on.

Solution 4

I would go with:

who | cut -d' ' -f1 | sort | uniq

This will show a list of real users. If the list is empty - machine is in logged out state, waiting for someone to log in.
ps aux will show also some system users, which you probably do not want to see.

Solution 5

Consider just setting the highest niceness for your calculations. Should avoid hogging the resources for any other users that may be logged in.

nice -n 19 your_calculation_command
Share:
7,125
sebastiangeiger
Author by

sebastiangeiger

Updated on September 18, 2022

Comments

  • sebastiangeiger
    sebastiangeiger over 1 year

    I need to run some calculations on a machine that I can only access remotely but also serves as a work station. There are many of them and I want to pick a "free" one. Free means in this case that no one is logged in locally.

    My first try was the who command, but for some reason it only lists "selected" users and I can't really find out how they are selected. Next try: ps aux | cut -d " " -f1 | sort | uniq: better showing a bunch of demons but also the local user that was not displayed by who.

    My current solution is to go in and do ps aux | grep "gnome-session" which is better but still gives me a lot of junk. Ideally I am looking for something that I can include in my ssh profile that warns me about (active) local users when I log in.

    EDIT:

    • Neither who nor w did return the local user. Is this an unexpected behaviour?
    • uptime on the other hand showed me the right amount of users (local and remote minus system users like root)
    • finger is not installed
    • Admin
      Admin almost 12 years
      I also see that w, who, and users are not returning the local users. This is new and unexpected behaviour on Ubuntu 12.04. I just rebooted some lab machines while (now very annoyed) users were logged on.
  • Andrejs Cainikovs
    Andrejs Cainikovs almost 12 years
    But you probably need to think of another scenario, when people is leaving their working places by hitting Ctrl+Alt+L, rather than logging out. I'm doing that! :)
  • sebastiangeiger
    sebastiangeiger almost 12 years
    Strangely enough who does not show me the local user. I know that he was actively working on that machine - he complained that I was clogging up the workstation. Is that who misbehaving?
  • ImaginaryRobots
    ImaginaryRobots almost 12 years
    depending on the network setup, you can also use finger to query remote machines without even logging in (though modern firewalls might have that feature blocked).
  • sebastiangeiger
    sebastiangeiger almost 12 years
    Creative, I like.
  • sebastiangeiger
    sebastiangeiger almost 12 years
    finger is not installed. There is a safe_finger that fails because it apparently relies on finger.
  • Eliah Kagan
    Eliah Kagan almost 12 years
    @sebastiangeiger Of course, it would be even better to periodically (say, every minute with a user crontab) check if there is a local user logged in, and if there is, decrease the niceness, and if there isn't, increase it (so as to get work done even when there are other remote users, or a system process, using a lot of CPU). Of course, to accomplish that, it would be necessary to have a reliable way to know if a local user is logged in. (You'd also have to overcome the problem that non-root users cannot renice a process down to a lower niceness, even the one it started at.)
  • Eliah Kagan
    Eliah Kagan almost 12 years
    @sebastiangeiger As Andrejs Cainikovs suggests, do you want to count a user as logged in locally if they have locked their workstation? You might not, as they are not right there interacting with the machine. But you might, because they still may be running programs and performing calculations (or whatever they do) that take CPU resources, which could take resources away from, and/or have their resources taken away by, your work. As a similar matter, do you want to consider users to be "logged in locally" if they are logged in locally on a virtual console, or only graphically?
  • sebastiangeiger
    sebastiangeiger almost 12 years
    @EliahKagan Ideally I want users running a local X session. It would be nice to know about users that locked the workstation or that are logged in via ssh, but my priority is to find out whether there is a person locally (and actively) working on it right now
  • sebastiangeiger
    sebastiangeiger almost 12 years
    @EliahKagan That usually does not happen, in fact I am sure that looking for a gnome-session would be suffice on those workstations 99% of the time.
  • sebastiangeiger
    sebastiangeiger almost 12 years
    Looks very promising, I'll have to check on Monday when there are people working at the office.