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

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

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´

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.


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.


last

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


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