When I shut down my computer I want to show some pending tasks that I have to do before leaving the office... I did a local application to manage those tasks, basically I just want to run a command, and shut down after I kill the app executed.


5 Answers 5


You could add your command to the file /etc/gdm/PostSession/Default before the exit 0 line.

  • If I go to "Log Out" option it executes fine, But If I press shutdown appear almost instantaneous ubuntu purple logout screen and executes the script (in the screen where appears ubuntu logo with 5 little balls). (tested with sleep 20).
    – juanefren
    Aug 5, 2010 at 16:33
  • The "Are you sure you want to shutdown" dialog is located here: /usr/lib/indicator-session/gtk-logout-helper and a part of the indicator-session package witch is written in C/C++ so its pretty hard to edit. You might have more luck asking at some gnome-guru mailinglist ;) Aug 10, 2010 at 18:21

You can do this by creating a script in /etc/rc0.d (for shutdown) and /etc/rc6.d (for reboot). To do this, run:

gksu gedit /etc/rc0.d/K01mycustomscript

Edit your shutdown script in the text editor then run:

sudo chmod +x /etc/rc0.d/K01mycustomscript

And if you want it to run on reboot also then run this:

sudo cp /etc/rc0.d/K01mycustomscript /etc/rc6.d/K01mycustomscript

Source: this forum thread

  • From his description it sounds likes he wants to invoke a graphical app, so it needs access to the xserver. In this case using init-scripts does not work.
    – sepp2k
    Aug 5, 2010 at 16:05
  • 1
    Yeah... as @sepp2k says, script is running outside xserver :(
    – juanefren
    Aug 5, 2010 at 16:36

Sounds like a question for superuser.com to me. Anyway, after some Googling I found How to run a script on logout? which says to add the script to $HOME/.bash_logout.

If that doesn't work, add


to /etc/gdm/PostSession/Default so it executes the logout script properly.

Seeing as before shutdown the user is logged off anyway, this should cover both bases.

  • 1
    Since he probably doesn't want the script to run every time, he logs out of bash (i.e. every time he closes a terminal window), I don't think it's a good idea to write the command into .bash_logout. However writing it into PostSession/Default looks like a good idea to me.
    – sepp2k
    Aug 5, 2010 at 16:04
  • Already have this file, but looks like it is not working (I put sleep 20 and it didn't wait to shutdown.)
    – juanefren
    Aug 5, 2010 at 16:31
  • .bash_logout will not run when you close a terminal, it will only run when you log out from a terminal-login-session (ie. if you logout a real TTY) and not when you logout of gnome. Aug 10, 2010 at 18:24
  • Why would this be a question for superuser? If the question is about Ubuntu it's on topic.
    – N.N.
    Aug 12, 2011 at 9:52

If you do ./app-to-run && sudo shutdown -h now the computer should shut down once app-to-run is done.

  • 1
    This would be a very crude shutdown. You rather want to call the desktop shutdown script instead.
    – txwikinger
    Aug 5, 2010 at 15:45
  • The sudo might ask the user to type the password in case app-to-run is long enough... So it'll never shutdown.
    – samb
    Sep 7, 2012 at 15:12

Almost same answer than Tommy Brunn but without the risk to hang with being asked the root password :

sudo sh -c "./app-to-run; shutdown -h now"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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