Execute Script on graphical logoff

Please post suggestions for improvement of Cinnamon on:
https://github.com/linuxmint/Cinnamon
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Post Reply
donbolli
Level 2
Level 2
Posts: 57
Joined: Mon Apr 24, 2023 2:21 pm

Execute Script on graphical logoff

Post by donbolli »

Hello everyone

I wrote a scrpt that works perfectly when I execute it manual. Now I want to execute this everytime my graphical interface logs off.

I found the use of ~/bash_logout, but that is only executed when the shell is quited. i want to execute when ever i log out of cinemon
Last edited by donbolli on Sun Mar 17, 2024 6:01 am, edited 2 times in total.
vimes666
Level 6
Level 6
Posts: 1241
Joined: Tue Jan 19, 2016 6:08 pm

Re: Execute Script on graphical logoff

Post by vimes666 »

I think you can put the script in /etc/X11/Xreset.d/
I never tried it though.
From /etc/X11/Xreset.d/README:
# Scripts in this directory are executed as root when a user log out from
# a display manager using /etc/X11/Xreset.
# The username of the user logging out is provided in the $USER environment
# variable.
donbolli
Level 2
Level 2
Posts: 57
Joined: Mon Apr 24, 2023 2:21 pm

Re: Execute Script on graphical logoff

Post by donbolli »

I tried it, it was not working. I assume I missed something, maybe rights or ownership?

It can be executed and it belongs to the default user
vimes666
Level 6
Level 6
Posts: 1241
Joined: Tue Jan 19, 2016 6:08 pm

Re: Execute Script on graphical logoff

Post by vimes666 »

Well, it was an estimated guess. I just tried it myself and indeed it didn't work.
donbolli
Level 2
Level 2
Posts: 57
Joined: Mon Apr 24, 2023 2:21 pm

Re: Execute Script on graphical logoff

Post by donbolli »

Okay, any other ideas?

X11 is not Cinamon as far as I know
mikeflan
Level 17
Level 17
Posts: 7162
Joined: Sun Apr 26, 2020 9:28 am
Location: Houston, TX

Re: Execute Script on graphical logoff

Post by mikeflan »

The only other idea I can think of is to broaden the question a bit.
What are you trying to fix?
donbolli
Level 2
Level 2
Posts: 57
Joined: Mon Apr 24, 2023 2:21 pm

Re: Execute Script on graphical logoff

Post by donbolli »

I do have script checking for a process (bitcoin-core), shuts it down so nothing get lost
billyswong
Level 8
Level 8
Posts: 2239
Joined: Wed Aug 14, 2019 1:02 am

Re: Execute Script on graphical logoff

Post by billyswong »

Related old tutorial post that may serve your need: viewtopic.php?t=270140
donbolli
Level 2
Level 2
Posts: 57
Joined: Mon Apr 24, 2023 2:21 pm

Re: Execute Script on graphical logoff

Post by donbolli »

This post worked, thank you.
donbolli
Level 2
Level 2
Posts: 57
Joined: Mon Apr 24, 2023 2:21 pm

Re: Execute Script on graphical logoff

Post by donbolli »

the script is only excuted on logoff - not on restart or shutdown. how can i achive that?
billyswong
Level 8
Level 8
Posts: 2239
Joined: Wed Aug 14, 2019 1:02 am

Re: Execute Script on graphical logoff

Post by billyswong »

donbolli wrote: Sun Mar 17, 2024 6:01 am the script is only excuted on logoff - not on restart or shutdown. how can i achive that?
Restart/shutdown doesn't imply logoff? Or you mean you want to delay the execution until shutdown, not everytime you logoff?

Another old post suggested using systemd service viewtopic.php?t=278535
But...
catweazel wrote: however these scripts, if any, are executed at the end of the shutdown process so you'll have to remount your drives if you need to write to them as they're read-only at this point.
If we stick with lightdm, there seems another hook for shutdown https://www.freedesktop.org/wiki/Softwa ... ystemhooks
Adding System Hooks

If you need some special behaviour when X servers and user sessions start/stop you can set commands to be run with the following configuration:

Code: Select all

[SeatDefaults]
display-setup-script=command
display-stopped-script=command
greeter-setup-script=command
session-setup-script=command
session-cleanup-script=command
session-wrapper=command
greeter-wrapper=command
display-setup-script is run after the X server starts but before the user session / greeter is run. Set this if you need to configure anything special in the X server. It is run as root. If this command returns an error code the X server is stopped.

display-stopped-script is run after an X server exits. It is run as root.

greeter-setup-script is run before a greeter starts. It is run as root. If this command returns an error code the greeter fails to start (which will cause LightDM to stop).

session-setup-script is run before a user session starts. If this command returns an error the session will not start (user is returned to a greeter).

session-cleanup-script is run after a greeter or user session stops. It is run as root.

session-wrapper is a the command to run for a session. This command is run as the user and needs to exec the command passed in the arguments to complete running the session. Use this if you need to do special setup for a user session. Note the default is 'lightdm-session' so you should chain to this if you need to override this setting.

greeter-wrapper is a the command to run a greeter. It is the equivalent of session-wrapper for greeters.
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Execute Script on graphical logoff

Post by AndyMH »

Thanks for that, I was aware of and use display-setup-script, I wasn't aware of the others.

If you want to know how to invoke those scripts from lightDM:
viewtopic.php?p=2209128#p2209128
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
User avatar
Qapla
Level 2
Level 2
Posts: 73
Joined: Tue Sep 14, 2021 5:35 pm
Location: San Francisco, 6,437 meters SouthEast of the future home of Starfleet Academy

Re: Execute Script on graphical logoff

Post by Qapla »

I am trying to do something similar, and can get the script to run at shutdown using either the session-cleanup-script or the systemd options, but they both run AFTER the GUI desktop is closed.

I want to run wmctrl -l -x > /home/$USER/Documents/wmctrl.txt to list open windows BEFORE the windows close.
How do I do that? Lightdm session-cleanup and systemd target multi-user are already too late.
Qapla' - Success! (Klingon salutation on departure to battle)
donbolli
Level 2
Level 2
Posts: 57
Joined: Mon Apr 24, 2023 2:21 pm

Re: Execute Script on graphical logoff

Post by donbolli »

From my understanding "session-cleanup-script" should have worked. But somehow it did not trigger the script on reboot or shutdown. display-stopped-script seems to be working.
donbolli
Level 2
Level 2
Posts: 57
Joined: Mon Apr 24, 2023 2:21 pm

Re: Execute Script on graphical logoff

Post by donbolli »

The script gets executed, but too late. The script needs to run on shutdown or logoff while the graphical interface is still active. Is it possible?
User avatar
AndyMH
Level 21
Level 21
Posts: 13759
Joined: Fri Mar 04, 2016 5:23 pm
Location: Wiltshire

Re: Execute Script on graphical logoff

Post by AndyMH »

donbolli wrote: Mon Mar 18, 2024 4:36 am The script needs to run on shutdown or logoff while the graphical interface is still active.
Track down the logoff and shutdown scripts, they are probably simple bash or python scripts which could be edited.
Thinkcentre M720Q - LM21.3 cinnamon, 4 x T430 - LM21.3 cinnamon, Homebrew desktop i5-8400+GTX1080 Cinnamon 19.0
billyswong
Level 8
Level 8
Posts: 2239
Joined: Wed Aug 14, 2019 1:02 am

Re: Execute Script on graphical logoff

Post by billyswong »

Some additional search lead me to an interesting example https://github.com/CGamesPlay/infra/blo ... ct.service

Code: Select all

[Unit]
Description=self destruct on poweroff

# We want to stop this service pretty late in the shutdown process, but
# before the network goes down. By setting Before=network.target, our self
# destruct will only happen after everything which is After=network.target.
Before=network.target user.slice machine.slice
# But the self destruct requires the network to actually be active.
After=systemd-networkd.service nss-lookup.target

[Service]
EnvironmentFile=-/etc/self-destruct.env
ExecStop=/usr/local/sbin/hcloud-self-destruct --force
Type=oneshot
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
Unlike this example, we want the script to run as early as possible in the logoff/shutdown procedure. So I suggest we skip the Before=, and use After=graphical.target instead.
User avatar
Qapla
Level 2
Level 2
Posts: 73
Joined: Tue Sep 14, 2021 5:35 pm
Location: San Francisco, 6,437 meters SouthEast of the future home of Starfleet Academy

Re: Execute Script on graphical logoff

Post by Qapla »

billyswong wrote: Mon Mar 18, 2024 8:23 am and use After=graphical.target instead.
No joy. I also tried adding an ExexStart=/bin/true since the suggested service includes RemainAfterExit.

I know my script IS being executed at Restart and/or Shutdown because I also have a touch /home/myid/kilroywashere.txt in it - and kilroy shows up. But again the graphical interface is already closed.

Journalctl shows the graphical interface closing almost immediately.

Code: Select all

Mar 18 11:34:34 P7Mint-VB systemd-logind[525]: System is rebooting.
Mar 18 11:34:34 P7Mint-VB systemd[1]: Stopping Session c2 of User linux...
Mar 18 11:34:35 P7Mint-VB systemd[1]: Removed slice Slice /system/modprobe.
Mar 18 11:34:35 P7Mint-VB systemd[1]: Stopped target Graphical Interface.
Mar 18 11:34:35 P7Mint-VB systemd[1]: Stopped target Multi-User System.
Mar 18 11:34:35 P7Mint-VB systemd[1]: Stopped target Login Prompts.
Mar 18 11:34:35 P7Mint-VB systemd[1]: Stopped target Sound Card.
Mar 18 11:34:35 P7Mint-VB systemd[1]: Stopped target Timer Units.
Then my script executes (and fails) milliseconds later. "listwindows" is my script.

Code: Select all

Mar 18 11:34:35 P7Mint-VB systemd[1]: Stopping wmctrl.service...
Mar 18 11:34:35 P7Mint-VB systemd[1]: Stopping Web Services Dynamic Discovery host daemon...
Mar 18 11:34:35 P7Mint-VB listwindows[2718]: Cannot open display.
Mar 18 11:34:35 P7Mint-VB systemd[1]: wmctrl.service: Control process exited, code=exited, status=1/FAILURE
Mar 18 11:34:35 P7Mint-VB systemd[1]: wmctrl.service: Failed with result 'exit-code'.
Mar 18 11:34:35 P7Mint-VB systemd[1]: Stopped wmctrl.service.
Finally I tried changing WantedBy=multi-user.target to WantedBy=graphical.target but that generated an error.

Code: Select all

Mar 18 12:39:37 P7Mint-VB systemd[1]: /etc/systemd/system/wmctrl.service:1: Assignment outside of section. Ignoring.
Any ideas welcome.
Qapla' - Success! (Klingon salutation on departure to battle)
User avatar
Qapla
Level 2
Level 2
Posts: 73
Joined: Tue Sep 14, 2021 5:35 pm
Location: San Francisco, 6,437 meters SouthEast of the future home of Starfleet Academy

Re: Execute Script on graphical logoff

Post by Qapla »

Gaaaaah! Sometimes the solution is so simple it is almost too simple to be obvious.

I wondered ... gee, what actually happens when I click the menu button and choose Quit (the red power button) ?
It runs a simple one line script: /usr/bin/cinnamon-session-quit

So, simply add your desired command(s) BEFORE the "exec" command. Mine is:

Code: Select all

#!/bin/sh
wmctrl -l -x > /home/$USER/Documents/wmctrl.txt
exec "/usr/share/cinnamon-session"/cinnamon-session-quit.py $@
I'm satisfied. Thank you. Qapla' (Success!)
Qapla' - Success! (Klingon salutation on departure to battle)
Post Reply

Return to “Cinnamon”