Get last login date for all users & Active users


As we try to show occ command user:lastseen , we cloud find an user last login as below.


How can we use this command for all user? Please kindly advise. Thank you in advance.

I just needed it today and wrote this small script. Make change accordingly and use it



cd $nextcloud_dir
sudo -u apache php occ user:list > $input

while IFS= read -r line
  username=`echo "$line" | awk '{ gsub(":",""); print $2}'`
  last_login=`sudo -u apache php occ user:lastseen $username |  awk '{ print $4 "-" $5'}`
echo "$line - $last_login" 
done < "$input"

Thank you for your information. We have tried it with OCC Web function,

which we just enabled in App

Is there any way we can do that command for all user via this OCC Web or not?

Thank you for your kind always support.

Its a bash script I posted, it wont work in OCC web session. You need to run it from SSH terminal. OCC web can just run occ commands.

das funktioniert soweit gut.
Aber bei einer Anmeldung als Anton Meier gibt es Probleme.
Kannst Du bitte es nochmal überprüfen?
Schön wäre es wenn man es auch anschließend als cron job laufen lassen kann um es bei Zeiten per Mail zu schicken

Danke im voraus

that works fine so far.
But there are problems when registering as Anton Meier.
The user name is Anton Meier.
Not only Anton.
With one Name it works fine.
But with complete Name no.
Can you please check it again?
It would be nice if you can also run it as a cron job afterwards so that you can send it by e-mail when the time comes

Thanks in advance

Sorry i could not get the old script to work. Maybe there is only a small error in the script e.g. "" or {} missing. But i never used it. :wink:

But you can use a from me minimal modified script i found here. Ok also this script i never used before because i do not need the feature. :wink: But it works with spaces in the names. :wink:


sudo -u www-data php /var/www/html/nextcloud/occ user:list --no-ansi --no-interaction | \
while read line
    user=`awk -F: '{print $1}' <<< "${line}" | awk -F"- " '{print $2}'`

    last_seen=`sudo -u www-data php /var/www/html/nextcloud/occ user:info --no-ansi --no-interaction "${user}" | \
               grep "last_seen:" | sed 's/^.*last_seen: *//'`

	echo "${user}:${last_seen}";

It would be a nice feature to get this information with occ user:list directly.

1 Like

Thank you.
It’s okay now.

Adding a capability to display all user’s last seen date and time would be fantastic, and I’ve opened a feature request for it.

This already exists:

Sure, plenty of scripts exist (and thank you for your addition, it looks good!) but what I’m saying is that none of this is in Nextcloud core, and it would be nice if it was :wink:

I’ll check out your script now. Thanks.

An update: Your script appears to show who is logged in right now (which would be handy for other things) but not so much a list of “when a user was last seen” which is what this thread is about.

You should look more thoroughly, it is sorted by last activity, which is “last seen” :wink:

Okay, but it seems unclear though. Your option description for that script says:

monitor                 monitor *live* who is logged in

Could you provide a sample output of nc-who monitor then?

i.e. Does it list all users (even those who are not logged in)? Because we’re looking for a list of the date and time all users last were.

Thanks :smile:

You should run it! That is what it is made for.

You will see all logins ever made, sorted by last activity if you want

Thanks for getting back, and the enthusiasm… but:

You should run it! That is what it is made for.

Well I tried running it, first I got sh: 7: html2text: not found error, so I figured it depends on html2text so I apt installed that. Got it to start.

But …

You will see all logins ever made, sorted by last activity if you want

… unfortunately, it didn’t do that for me.

I tried option [2] first (show all), but only got list of 1 user with two logins, where 6 users in total exist on the server:

| all logins, refresh rate = 10 s. [s] change refresh rate - [r] reload - [b] back - [q] quit
| last_activity       | last_check          | uid  | client                         |
| 2023-06-25 06:52:05 | 2023-06-25 06:49:22 | joe | dev1 (Desktop Client - Linux)   |
| 2022-04-02 20:02:24 | 2022-04-02 20:01:16 | joe | dev2 (Desktop Client - Windows) |

So I tried option [1], cookie based login, and it returned a blank screen.

I appreciate what your effort and doings, but this is what occ does:

$ sudo -u www-data php /var/www/nextcloud/occ user:list
  - jennyk: Jenny Kenston
  - irene: Irene
  - joe: Joe
  - lukas: Lukas
  - dane: Dane
  - test: Test

… and this is what I’d want it to do (going back to where this thread started):

$ sudo -u www-data php /var/www/nextcloud/occ user:lastseen --all
jennyk`s last login: 20.06.2023 23:09
irene`s last login: 30.03.2022 11:30
joe`s last login: 29.06.2023 04:21
lukas`s last login: 27.06.2023 10:22
dane`s last login: 26.05.2023 10:21
test`s last login: 21.06.2023 18:03


occ has --output-json parameter which allows easy post-processing with jq command…

here is working example showing all user with their last_seen date

1 Like

But that would be a simple iteration!

This does the job with simple admin household remedies:

# change path and webserver-user accordingly:
occ_call="sudo -u www-data php -f /var/www/nextcloud/occ"

# We need to take the detour via base64 to allow usernames with spaces
for user in $(echo $($occ_call user:list --output=json | jq -r 'keys_unsorted[] | @base64')); do
    user="$(echo "$user" | base64 --decode)"
    $occ_call user:lastseen "$user"

Much luck

Thanks, and sure, one can do this (after installing jq by-the-way) with bash scripts to do iterating, but none of this is an ask for that.

I’m more just simply asking for an --all type switch (like other occ commands have) so this becomes a built-in, to save all this mucking around :smiley: