run a bash script at logoff, shutdown, and restart on Linux

3

I need to run a bash script each time a Debian-esque thick client logs off, shuts down, or restarts. Outside of that, it can be any distro, desktop environment, window manager, or otherwise (preferably not Unity or Gnome 3; these are older machines), so long as it can do what I need.

I have already tried Lubuntu 12.04, without any luck.

Beefy_Swain

Posted 2013-06-11T15:07:31.597

Reputation: 361

Answers

4

You need to look into run levels, and how upstart ties in with them. This page explains a lot about two of the three stages you are interested in, shutdown and reboot, log off should also just be a simple hook.

NickW

Posted 2013-06-11T15:07:31.597

Reputation: 1 029

"log off should also just be a simple hook" can you expand on that? What do you mean hook, exactly? I knew about run levels relating to shutdown and reboot, but I did not think that logging off was a different run level. – Beefy_Swain – 2013-06-11T15:33:57.940

1

Sorry, I was trying to state that it wasn't necessarily a change in run levels, you might be able to trigger off an event from a job stopping.. or from a custom triggered upstart instance for your user : http://upstart.ubuntu.com/cookbook/#upstart-user-sessions-spec

– NickW – 2013-06-11T15:45:16.450

I see. If I trigger from an event stopping, how would my script be guaranteed to execute before it gets stopped? – Beefy_Swain – 2013-06-11T15:51:13.110

1If you use the Method Event, you can give your script parameters, as they say on that page: "The caller waits for the work to be done, and if problems were encountered, it expects to be informed of this fact.". – NickW – 2013-06-11T15:55:55.580

Alright, Thanks. If I have more questions I'll post a second question, as im getting away from the original. – Beefy_Swain – 2013-06-11T15:57:19.023

1Yeah, it's a seriously large concept, which is why I really didn't try to get into too much detail :) – NickW – 2013-06-11T15:58:06.213