Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: How can I run a logout script whenever I log out?

  1. #1
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,772
    Distro
    Lubuntu 18.04 Bionic Beaver

    Question How can I run a logout script whenever I log out?

    I want a specific script to run every time I log out, whether it's via Settings > Power Off/Log Out > Log Out (so the computer remains on) or Settings > Power Off/Log Out > Power Off… > Restart or Power Off.

    It mustn't run if I merely Suspend or Switch User.

    The script is extremely simple, quick, and just for the one user, not for any other user on the system.

    The answers that I've found for Ubuntu 20.04 or Gnome 3 say to edit either ~/.bash_logout, which doesn't run (even if I make it executable), or /etc/gdm/PostSession/Default, but the folder /etc/gdm doesn't exist.

    I've been searching for ages, but I can't find an answer.

    How can I run a logout script when I log out?

    I'm using Ubuntu 20.04 with Gnome 3.36.3.

  2. #2
    Join Date
    Jul 2005
    Location
    I think I'm here! Maybe?
    Beans
    Hidden!
    Distro
    Xubuntu 20.04 Focal Fossa

    Re: How can I run a logout script whenever I log out?

    To some extent the possibilities for this, and how you might be able to do it will depend on what your script does, how long it takes, and how you currently shutdown.

    I use a keyboard shortcut to shutdown my Xubuntu machine with the Super key (Windows) + Pause/Break keys which run command xfce4-session-logout --halt to which I could easily add a script to run first, then shutdown the machine. I have no idea what command the shutdown button itself runs as I've not needed to find it but it may be possible for you to do something along the lines of my shutdown system if you can find out the command that you currently use.

    If you can find the command for the method you use now just run your script but add the shutdown command as its final command.

  3. #3
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: How can I run a logout script whenever I log out?

    I have found that AutoKey can be useful for such tasks.
    Assign a hot key to run a custom python script in AutoKey saved in ~/.config/autokey/data/
    Use subprocess in python script to run the command(s).

  4. #4
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,772
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: How can I run a logout script whenever I log out?

    Thank you for your idea. I might have to take it up, although I was trying to avoid that.

    The reason is that it's a bit more complicated…

    Ubuntu 20.04 doesn't have a guest account, due to some problem that I don't understand. So, I've set up an account intended to be a guest account. On logging out, the script replaces the entire contents of the guest account with a mostly-empty template to reset the account (the template has been marked immutable). That's why I want this script to run on logout.

  5. #5
    Join Date
    Mar 2011
    Location
    U.K.
    Beans
    Hidden!
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: How can I run a logout script whenever I log out?

    On logging out, the script replaces the entire contents of the guest account with a mostly-empty template to reset the account (the template has been marked immutable). That's why I want this script to run on logout.
    But .. can't you use AutoKey to run your script above (use python subprocess) and then logout?

  6. #6
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,772
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: How can I run a logout script whenever I log out?

    Quote Originally Posted by dragonfly41 View Post
    But .. can't you use AutoKey to run your script above (use python subprocess) and then logout?
    Probably I could, but because it's a guest account, I can't ensure that whoever uses it will use that autokey. I'd have to somehow tie the command it to the built-in menu options for logout, restart or power-down.

  7. #7
    Join Date
    Apr 2014
    Beans
    773

    Re: How can I run a logout script whenever I log out?

    I'm running Xubuntu so I don't have Gdm. Is it /etc/gdm3 by chance? I used to have some auto scripts there if I recall when I did use Gnome. Maybe typo or slight oversight?

  8. #8
    Join Date
    Jan 2017
    Beans
    205

    Re: How can I run a logout script whenever I log out?

    Quote Originally Posted by Tadaen_Sylvermane View Post
    ... Is it /etc/gdm3 by chance? ...
    Yes. https://packages.ubuntu.com/focal/amd64/gdm3/filelist

    I have a script in /etc/gdm3/PostSession/Default that runs on logout.
    Last edited by norobro; October 2nd, 2020 at 03:22 PM. Reason: corrected typo /etc/gdm/PostSession/Default

  9. #9
    Join Date
    May 2008
    Location
    United Kingdom
    Beans
    4,772
    Distro
    Lubuntu 18.04 Bionic Beaver

    Re: How can I run a logout script whenever I log out?

    @Tadaen_Sylvermane and @norobro

    Thank you. First, a face palm for me, not thinking of looking at /etc/gdm3

    I've tested this.

    /etc/gdm3/PostSession/Default works if I log out but don't restart. However, it fails to run if I restart.

    So, it's part way there — it runs when logging out without restarting, but it doesn't run when logging out by restarting.

    Do you have another idea?

  10. #10
    Join Date
    Jan 2017
    Beans
    205

    Re: How can I run a logout script whenever I log out?

    Found a Redhat bug report that says that this was fixed upstream a year ago but it doesn't work for me either. https://bugzilla.redhat.com/show_bug.cgi?id=1547158

    Would a systemd service work? Something like this:
    Code:
    [Unit]
    Description=Run at reboot
    DefaultDependencies=no
    Before=reboot.target
    
    
    [Service]
    Type=oneshot
    ExecStart=/path/to/reboot_script.sh
    TimeoutStartSec=0
    
    
    [Install]
    WantedBy=reboot.target

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •