Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Есть проблема на Ubuntu 11.10, при запуске sh скриптов, без активной консоли, команда who не отображает активных пользователей, а в скрипте необходимо знать, какой пользователь активен сейчас. Кто нибудь сталкивался?

(Google Translate's courtesy)

There is a problem on Ubuntu 11.10, when you run the sh script, without the active console, the team who does not show active users, but the script needs to know which user is active right now. Who ever came across?

By the way acpi package has a bug related to my problem, its script is also used by the command who.

$ dpkg -l | grep acpi | awk '{print $2 "_" $3}'

$ grep who /usr/share/acpi-support/*
/usr/share/acpi-support/power-funcs: user=$(who | awk "/:$displaynum)/ { print \$1; exit }")
/usr/share/acpi-support/power-funcs: user=$(who | awk "/:$displaynum/ { print \$1; exit }")

I think this can be fixed:

user=$(users | sed 's | |\n|g' | uniq)

____ Updated 12.01.2012
The problem is not solved!
Small amusing experiment: for the test with who and user commands, I run the terminal, and add the following lines to /etc/crontab:

* * * * *   root    echo 'who: '`who` >> /123
* * * * *   root    echo 'users: '`users` >> /123
* * * * *   root    echo 'users /var/run/utmp: '`users /var/run/utmp` >> /123
* * * * *   root    echo 'users /var/log/wtmp: '`users /var/log/wtmp` >> /123

And check the /123 file when terminal is running:

users /var/log/wtmp: administrator administrator...
who: administrator pts/3 2012-01-12 14:59 (:0.0)
users: administrator
users /var/run/utmp: administrator

when terminal is closed:

users /var/run/utmp: 
users /var/log/wtmp: administrator administrator ...

Again, the question of how to get a list of users logged on to the system (X session), from the console as root.

Until I see only one solution:

user=$(users /var/log/wtmp | sed 's| |\n|g' | uniq)`

Who knows a more elegant solution? Or is this just an option?
Or maybe it's still a bug?

share|improve this question
Welcome to Askubuntu. Please use English to get help from this community. Thanks :) – Achu Jan 7 '12 at 7:08
up vote 3 down vote accepted

The currently logged-in user is set in the environment variable $USER

echo $USER

This should give you all you need. For a list of all users logged-in run

share|improve this answer
I need use in my script notify-send command, and run script as root, value $USER return root, but i need run notify-send as logged user with command: su user -l -c "DISPLAY=:0.0 /usr/bin/notify-send -t 500 'text'" – METAJIJI Jan 7 '12 at 10:26
Takkat, thank a lot! users this is what I need! Sending notices to all users get this:<br/> #!/bin/sh for user in $(users): do su $user -l -c "DISPLAY=:0.0 /usr/bin/notify-send -t 500 'text'" done<br/> p.s. Sorry for my english – METAJIJI Jan 7 '12 at 10:48
By default, the program users reads data from /var/run/utmp but there is no relevant information and program users not show logged users... But if you specify the /var/log/wtmp, then the program begins to provide data. Finnaly we get a construction: users /var/log/wtmp | sed 's| |\n|g' | uniq – METAJIJI Jan 12 '12 at 7:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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