22

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.

juanefren
  • 355

5 Answers5

7

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

popey
  • 23,667
  • 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 05 '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 ;) – LasseLuttermann Aug 10 '10 at 18:21
7

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

Lincity
  • 25,371
dv3500ea
  • 37,204
  • 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 05 '10 at 16:05
  • 1
    Yeah... as @sepp2k says, script is running outside xserver :( – juanefren Aug 05 '10 at 16:36
4

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

$HOME/.bash_logout

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.

myusuf3
  • 34,189
  • 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 05 '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 05 '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. – LasseLuttermann 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 09:52
2

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

Tommy Brunn
  • 9,751
  • 1
    This would be a very crude shutdown. You rather want to call the desktop shutdown script instead. – txwikinger Aug 05 '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 07 '12 at 15:12
1

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"
samb
  • 1,306