Xfce Forum

Sub domains
 

You are not logged in.

#1 2018-11-06 08:35:02

yonux
Member
Registered: 2018-11-06
Posts: 7

[Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

Hi,

Linux Distrib : Ubuntu 18.04.1 LTS
login manager : xdm 1:1.1.11-3ubuntu1

I searched a lot this forum and others for days now, but I can't find the answer to my problem.


Here is what I want to achieve : log timestamps of user's login and logout

In the current configuration it is working.
What did I do ?
- created : "/etc/X11/Xsession.d/97x11-login_user_to_sql"
content :
#!/bin/sh
echo "INSERT INTO timetable VALUES (null,null,'I','$USER');" | mysql -u elapsed -p"elaspedtime" -U elapsed -h server -v

- created : "/etc/X11/Xreset.d/11x11-logoff_user_to_sql"
content :
#!/bin/sh
echo "INSERT INTO timetable VALUES (null,null,'O','$USER');" | mysql -u elapsed -p"elaspedtime" -U elapsed -h server -v

mysql's database is :
+------+---------------------+----------+--------+
| clef | stime               | activity | user   |
+------+---------------------+----------+--------+
|    1 | 2018-10-24 18:59:54 | I        | yonux |
|    2 | 2018-10-24 19:00:01 | O        | yonux |


It is working well when (and ONLY when) user logs in and logs out.
But, it is not when user logs in and then clicks on "shutdown" in the menu, instead of "logout".

After two "login and shutdown", we can see that the "logout" script is never executed :
|    3 | 2018-10-28 18:16:59 | I        | yonux |
|    4 | 2018-11-01 07:56:17 | I        | yonux |


As far as I understood, xfce4-session-logout is called at logout, and so, I think it is responsible of this situation.

At logout, after it has ended the xfce4 session, xdm finishes its todo-list executing what's in /etc/X11/Xreset.d/ for example), then pops back on the login screen.

But when an user clicks on "shutdown" all processes seem to be killed as fast as possible without a way to have anything executed ...

The only thing I am thinking about is write a shell wrapper script to xfce4-session-logout and do things before launching the real binary.
But it is not solid enough for me, because what happens to my wrapper when packages are updated ? Wrapper will be replaced by the binary and/or a warning/error message will appear. Leading the system in the same situation it was before the wrapper.

Is there an official and clean way to achieve that ?

Last edited by yonux (2018-11-13 15:42:43)

Offline

#2 2018-11-06 12:03:37

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

yonux wrote:

Hi,

Hello and welcome.

But when an user clicks on "shutdown" all processes seem to be killed as fast as possible without a way to have anything executed ...

There are a number of existing bug reports specific to this functionality. For example:
- https://bugzilla.xfce.org/show_bug.cgi?id=13445
- https://bugzilla.xfce.org/show_bug.cgi?id=10295

The only thing I am thinking about is write a shell wrapper script to xfce4-session-logout and do things before launching the real binary.
But it is not solid enough for me, because what happens to my wrapper when packages are updated ? Wrapper will be replaced by the binary and/or a warning/error message will appear. Leading the system in the same situation it was before the wrapper.

If you use /usr/local/bin/xfce4-session as your wrapper script, assuming that /usr/local/bin is earlier in your $PATH and most distros do, it would execute first and would not be over-written when package updates. Something like:

#!/bin/bash

#execute real binary
/usr/bin/xfce4-session

#execute scripts on logout
/your/scripts/here

Is there an official and clean way to achieve that ?

It should be fixed in the code. See for example:
- https://bugzilla.xfce.org/show_bug.cgi?id=2382
- https://bugzilla.xfce.org/show_bug.cgi?id=10172


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#3 2018-11-06 12:49:28

yonux
Member
Registered: 2018-11-06
Posts: 7

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

Thanks for the quick (and helpful) reply !

Wrapper to xcfe4-session ... interesting, I must give it a try.

Thank you again, I am going to test this and will come back here to explain what I have done.

Offline

#4 2018-11-06 14:08:43

yonux
Member
Registered: 2018-11-06
Posts: 7

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

Not better ...

I have :
- disabled /etc/X11/Xreset.d/11x11-logoff_user_to_sql
- wrote the wrapper in /usr/local/bin/ and added the SQL insert

Tests ("ps -elf |grep session" shows that the wrapper has been called) :
login and logout : OK
login and kill of xfce4-session : OK
login and shutdown : NOT OK

So, it is so quick that I think systemd is involved in the process.
But, as a Linux veteran user, I have no idea of what to do in systemd ("where is my old init ?" ok, I stop, I don't want to release the Troll) ...

I read the bug you were talking about : https://bugzilla.xfce.org/show_bug.cgi?id=10172
But the workaround described (the wrapper I tried) is not working any more, certainly due to systemd, but it is just an idea, I have no proof of that.

Offline

#5 2018-11-06 14:56:26

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

I tested the following script on Xubuntu 18.04 and it worked for me (worked in that the log entries were created):

#!/bin/bash

echo "Entering..." >> /tmp/session.log

/usr/bin/xfce4-session

echo "...Leaving" >> /tmp/session.log

If your script looks similar, maybe the script dies and the session ends before the sql queries get written. Try adding a delay (sleep) to the end of the file to give it time to complete.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#6 2018-11-07 07:30:35

yonux
Member
Registered: 2018-11-06
Posts: 7

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

So you were right, the SQL insert is taking too long time to be executed before being killed.

My tests :
Like you, if I just want to add a line in a local file, it is working.
But, if I do a "sleep 10", for example, it is waiting on "logoff" but not on "shutdown".

That's why I am looking for a way to have this process not killed before a certain amount of time (should be something in systemd configuration, somewhere, I hope).

Offline

#7 2018-11-07 12:29:21

yonux
Member
Registered: 2018-11-06
Posts: 7

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

Solved.

It is not really an issue related to XFCE4 but more to xdm (I did not try others dispaly managers) or/and systemd.
Actually, the scripts in /etc/X11/Xreset.d/ are certainly meant to be executed between two sessions, in order to reset the environment, so si it really an issue ? ...


For the record, here is what I have done :


The login script in /etc/X11/Xsession.d/97x11-login_user_to_sql :

#!/bin/sh
echo "INSERT INTO timetable VALUES (null,null,'I','$USER');" | mysql -u elapsed -p"elasped" -U elapsed -h server -v

The logout script in /etc/X11/Xreset.d/11x11-logoff_user_to_sql :

#!/bin/sh
if [ $USER ]; then
  echo "INSERT INTO timetable VALUES (null,null,'O','$USER');" | mysql -u elapsed -p"elasped" -U elapsed -h server -v
fi

An simple systemd script stored in /etc/init.d/wait_xdm.sh :

#!/bin/sh

### BEGIN INIT INFO
# Provides:             wait_xdm
# Required-Start:       $local_fs $remote_fs
# Required-Stop:        $local_fs $remote_fs
# Should-Start:
# Should-Stop:
# Default-Start:        S
# Default-Stop:
# Short-Description:    Gives 2 seconds to XDM before kill
### END INIT INFO

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
NAME="wait_xdm"
DESC="Gives 2 seconds to XDM before kill"

test -x /bin/sleep || exit 0

if [ -r "/etc/default/${NAME}" ]
then
        . "/etc/default/${NAME}"
fi

. /lib/lsb/init-functions

set -e

case "${1}" in
        start)
                /bin/true
                ;;

        stop)
                /bin/sleep 2
                ;;

        restart|force-reload)
                /bin/true
                ;;

        *)
                echo "Usage: ${0} {start|stop|restart|force-reload}" >&2
                exit 1
                ;;
esac

exit 0

(would be great add the sleep value in /etc/default/wait_xdm but I am a lazy boy)

Right after that, we have to modify the /etc/init.d/xdm script to add "wait_xdm" dependency, like this :

### BEGIN INIT INFO
# Provides:          xdm
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs wait_xdm
# Should-Start:      xfs $named slapd
# Should-Stop:       xfs $named slapd
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: The X Display Manager
### END INIT INFO

Then it is time to enable the service, and start it :

~# systemctl enable wait_xdm.sh
wait_xdm.sh.service is not a native service, redirecting to systemd-sysv-install.
Executing: /lib/systemd/systemd-sysv-install enable wait_xdm.sh
~# service wait_xdm status
● wait_xdm.service - LSB: Gives 2 seconds to XDM before kill
   Loaded: loaded (/etc/init.d/wait_xdm.sh; generated)
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)
~# service wait_xdm start
~# service wait_xdm status
● wait_xdm.service - LSB: Gives 2 seconds to XDM before kill
   Loaded: loaded (/etc/init.d/wait_xdm.sh; generated)
   Active: active (exited) since Wed 2018-11-07 13:01:37 CET; 1s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 2020 ExecStart=/etc/init.d/wait_xdm.sh start (code=exited, status=0/SUCCESS)

nov. 07 13:01:37 geb systemd[1]: Starting LSB: Gives 2 seconds to XDM before kill...
nov. 07 13:01:37 geb systemd[1]: Started LSB: Gives 2 seconds to XDM before kill.
~#

I hope this will help somebody.

Cheers !

Offline

#8 2018-11-07 13:16:46

ToZ
Administrator
From: Canada
Registered: 2011-06-02
Posts: 11,486

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

Thanks for sharing the solution.


Please remember to mark your thread [SOLVED] to make it easier for others to find
--- How To Ask For Help | FAQ | Developer Wiki  |  Community | Contribute ---

Offline

#9 2018-11-13 15:41:53

yonux
Member
Registered: 2018-11-06
Posts: 7

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

Well, sadly this is not working as expected.

The good results seen are just "luck" and so, not consistent.
Sometimes the command is executed, sometimes not.

So, as referred before, the main issue seems to be related in this XFCE4 bug :
https://bugzilla.xfce.org/show_bug.cgi?id=10295

I am going to feed it with my thoughts and feelings ...

Offline

#10 2018-11-13 16:07:51

yonux
Member
Registered: 2018-11-06
Posts: 7

Re: [Bug #10295] Execute a script at logon and logoff (logoff or shutdown)

What I am working on at the minute (but without any luck so far) :

- I removed all tuning of systemd's scripts
- scripts added to xdm configuration had been removed too
- I came back using a wrapper to xfce4-session which looks like this :

#!/bin/bash
# A simple wrapper to /usr/bin/xfce4-session in order to do things just BEFORE the users' session
# and after (in case of logout AND shutdown)

# First, let's define a function in order to catch
# the incoming SIG (TERM or INT) in case of a shutdown
exit_script() {
    echo "INSERT INTO timetable VALUES (null,null,'O','$USER');" | mysql -u elapse -p"elaspe" -U elapsed -h server
    trap - SIGINT SIGTERM # clear the trap
    kill -- -$$ # Sends SIGTERM to child/sub processes
}

trap exit_script SIGINT SIGTERM

echo "INSERT INTO timetable VALUES (null,null,'I','$USER');" | mysql -u elapsed -p"elaspe" -U elapse -h server

# call /usr/bin/xfce4-session
/usr/bin/xfce4-session

# Logout
echo "INSERT INTO timetable VALUES (null,null,'O','$USER');" | mysql -u elapsed -p"elaspe" -U elapsed -h stargate

As usual, it is working well on login/logout sequence, but not on login/shutdown.
It is (nearly ?) impossible to have systemd to wait for a script when it has been asked to shutdown the machine.

So, I rose the idea of adding a "run on logout feature" : https://bugzilla.xfce.org/show_bug.cgi?id=10172

Hopping somebody will help on this ...

Offline

Registered users online in this topic: 0, guests: 1
[Bot] ClaudeBot

Board footer

Powered by FluxBB
Modified by Visman

[ Generated in 0.009 seconds, 8 queries executed - Memory usage: 599.66 KiB (Peak: 616.51 KiB) ]