I want to run a script before I log out of my GNOME session, to warn myself if I have forgotten a pendrive plugged into the machine.
However, all solutions I have found have not been enough for me:
-
This similar question suggests editing the
/etc/gdm/PostSession/Default
file, but I don't have the permissions to do that. (And the file is currently empty on my machine, so there are no existing hooks to take advantage off) -
I also found this post suggesting a workaround but, according to the first reply, it does not work if I shutdown the computer instead of only logging off.
Best Answer
As you are using Gnome, you can use the Python script below, adapted from the one you mentioned.
It needs a Gnome logout (ie
gnome-session-quit
) (or gnome shutdown), the one happens when we use the logout in GUI. AFAIK no process can block a shutdown by the commandsudo shutdown -h 0
or bysudo poweroff
. Whenshutdown
is execued it gives SIGTERM to all process and gives a few seconds to them to exit (After executing some scripts which can't be edited by non root users ). If not exited processes are forcefully killed by SIGKILL.This is the step by step procedure of
gnome_save_yourself
method. Let's do a test.Save following code as
~/Desktop/execute_script_on_shutdown.sh
(From http://www.linuxquestions.org/questions/linux-desktop-74/gnome-run-script-on-logout-724453/#post3560301)Make it executable:
Save the following as
~/Desktop/shutdown_script.sh
Execute the main script
Now you feel the script wait for something
Check for a file named
AAAAAAAAAAAAAAAAAAAAAAAAAAA
on your desktop.If you see the file everything OK. Now you can edit the
shutdown_script.sh
to suit your need. Also remember to execute theexecute_script_on_shutdown.sh
on login (or make it auto executable on startup).