Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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.

share|improve this question
    
Put symlinks to executables in /etc/init.d in /etc/rc0.d, and /etc/rc6.d –  Yet Another User May 10 '13 at 1:21

2 Answers 2

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

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:

share|improve this answer
2  
It's conventional to name scripts starting with K for 'Kill' and S for 'Start'. –  minerz029 Oct 27 '13 at 13:00
    
What if the script needs sudo privileges? As far as I can tell, the /etc/rc6.d scripts are run as user, and cannot use gksudo. –  Urhixidur Aug 13 at 17:10
    
doesn't work for me with 14.04, is there any change or unspecified important fact? –  tomasb Oct 1 at 0:35
1  
K99 advice is not good for shutdown/reboot if you want to save some data because it happens AFTER unmount AND K is used to disable script (see /etc/rcS.d/README), use S for start and place it before unmounts, for bootup place your script after mount, got stuck on this because this misinformation, resolved with S as the first letter –  tomasb Nov 18 at 18:18

Use init.d. Put the script in /etc/init.d and link it to the proper run level. See http://www.debian-administration.org/articles/212

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.