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

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
1  
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
3  
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 '14 at 17:10
1  
doesn't work for me with 14.04, is there any change or unspecified important fact? –  tomasb Oct 1 '14 at 0:35
2  
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 '14 at 18:18
2  
This is very wrong: i) script names need to start with a K for runlevels 0, 1 and 6 and S for runlevels 2, 3, 4 and 5; ii) you don't need to call the script K99_foo, the number just determines the order the scripts are run in. You need to chose one that is larger than the others if you want it to be run last; iii) Runlevel 0 is not for reboot!; It's for shutdown. 6 is for reboot. iv) scripts have no business being in the /etc/rcN.d directories. They should instead be in /etc/init.d and linked to /etc/rcN.d. –  terdon Jul 24 at 11:11

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.