Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
May be useful for future users: – Serg Apr 22 at 14:56

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

share|improve this answer
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 '10 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 ;) – LassePoulsen Aug 10 '10 at 18:21

Sounds like a question for 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.

share|improve this answer
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 '10 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 '10 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. – LassePoulsen Aug 10 '10 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 '11 at 9:52

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

share|improve this answer
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 '10 at 16:05
Yeah... as @sepp2k says, script is running outside xserver :( – juanefren Aug 5 '10 at 16:36

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

share|improve this answer
This would be a very crude shutdown. You rather want to call the desktop shutdown script instead. – txwikinger Aug 5 '10 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 '12 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"
share|improve this answer

Your Answer


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.