1

I want to log the logins/logouts of my users.

For the logins I only need to modify /etc/profile and add my command or edit any .profile for an specific user.

But for the logouts I don't know if there is a default/centralized way to do it like /etc/profile for logins.

I see some example for CLI sessions, but I want to register GUI/CLI sessions.

Thanks

Zanna
  • 69,223
  • 56
  • 216
  • 327
raultm
  • 111
  • 3
  • I'm not sure you can rely on /etc/profile for non-shell sessions in general - even for logins. To do this properly, you'll probably need to use PAM. See for example [Hook into linux authentication to run script or program when certain conditions are met](https://stackoverflow.com/questions/5284624/hook-into-linux-authentication-to-run-script-or-program-when-certain-conditions) or another project that appears to be more active [pam-hooks](https://upsilon.cc/~zack/hacking/software/pam-hooks/) – steeldriver Feb 16 '22 at 13:08
  • thanks for the comment, i've been using /etc/profile without problems and works smoothly. Nevertheless i'll check it out your recommendation – raultm Feb 16 '22 at 13:12
  • I'm not saying `/etc/profile` can't work - it's just that it relies on the user's X (or Wayland etc.) session being started as a child of a Bourne-type login shell - which is not a given. See for example [X session wrapper](https://wiki.archlinux.org/title/LightDM#X_session_wrapper). – steeldriver Feb 16 '22 at 13:22
  • Actions upon logout won't run in the event of a crash or system reset or power loss. So you must also create a check at login to discover whether the logout job ran. – user535733 Feb 16 '22 at 13:34
  • Does this answer your question? [execute script when closing terminal](https://askubuntu.com/questions/1021971/execute-script-when-closing-terminal) – Artur Meinild Feb 17 '22 at 08:57

1 Answers1

1

Found this thread: https://unix.stackexchange.com/questions/208277/how-can-i-set-a-bash-logout-for-all-users

From the manual (man bash):

When a login shell exits, bash reads and executes commands from the files ~/.bash_logout and /etc/bash.bash_logout, if the files exists.
CPH
  • 368
  • 2
  • 9
  • that is for CLI not for GUI, isn't it? – raultm Feb 17 '22 at 11:51
  • I know it works for CLI. Not sure about GUI however. If you are wanting specifically for GUI I must have misunderstood the question. I'm not sure of in that case – CPH Feb 17 '22 at 15:42