Ubuntu – Execute a script upon logout/reboot/shutdown in Ubuntu

logoutrebootscriptsshutdown

I'd like to run a .sh file using bash on logout and shutdown of Ubuntu. I have searched up and down the web and have found only fixes for KDE and GNOME. The script is simple, I just need to know if I can add the line of code to the "logoff/shutdown" file or if I need to reference it in another script.

Best Answer

  • To execute a script upon logout:

    Add the following line:

    session-cleanup-script=/path/to/script
    

    in /etc/lightdm/lightdm.conf file.

    You have to restart lightdm in order that this change to take effect. To do this go in tty1 using Ctrl+Alt+F1, login with your username and password and run the following command:

    sudo service lightdm restart
    

    Note: use Ctrl+Alt+F7 to switch to the graphical session

    To execute a script upon reboot:

    1. Put your script in /etc/rc0.d
    2. Make it executable: sudo chmod +x K99_script
    3. Notes:
      • The scripts in this directory are executed in alphabetical order.
      • The name of your script must begin with K99 to run at the right time.

    To execute a script at shutdown:

    1. Put your script in /etc/rc6.d
    2. Make it executable: sudo chmod +x K99_script
    3. Notes:
      • The scripts in this directory are executed in alphabetical order.
      • The name of your script must begin with K99 to run at the right time.

    Sources:

  • Related Question