As you are using gnome you can use the following link answered by me ( python script you early mentioned with slight mod & detailed info)
http://askubuntu.com/a/404966/123897
It need 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 command sudo shutdown -h 0 or by sudo poweroff. When shutdown is execued it gives SIGNTERM 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 SIGNKILL.
totti
- 1.5k
- 15
- 15