6

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
1
  • 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.
    – user71375
    Jun 18, 2012 at 8:29

8 Answers 8

7

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´
4

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.

3
  • 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). May 31, 2012 at 21:57
  • finger is not installed. There is a safe_finger that fails because it apparently relies on finger. Jun 1, 2012 at 5:51
  • 1
    @sebastiangeiger safe_finger is present as part of the tcpd package regardless of whether or not the finger package (which provides the finger command, which safe_finger is just a wrapper for) is actually installed. It's advisable to use safe_finger when contacting finger servers on remote machines (especially ones you don't yourself administer and ensure don't cause the client-side problems safe-finger protects against). If you're using finger locally, it shouldn't be necessary to use safe-finger. Jun 1, 2012 at 6:52
2

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.

6
  • 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! :) May 31, 2012 at 17:41
  • 1
    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? May 31, 2012 at 18:44
  • @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? Jun 1, 2012 at 6:12
  • @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 Jun 1, 2012 at 8:14
  • @sebastiangeiger What about users logged in locally via text-based virtual console? Jun 1, 2012 at 8:52
2

last

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

1
  • Looks very promising, I'll have to check on Monday when there are people working at the office. Jun 2, 2012 at 18:04
1

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
2
  • Creative, I like. Jun 1, 2012 at 5:32
  • 1
    @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.) Jun 1, 2012 at 6:09
1

My answer to list every user logged, in unique way.

w | tail -n +3 | cut -d " " -f1 | sort | uniq
0

A quick and dirty attempt:

ps au --no-heading | cut -d " " -f1 | uniq
0

I like this one:

for i in $(ls /dev/pts/*);do echo "TTY $i - $(ps e -t $i|grep -oP "(?<=(RUSER\=))[a-z]*"|uniq)";done ; echo -e '\n+++\n';  for i in $(pgrep ssh);do pstree -paul $i;done

You must log in to answer this question.

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