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

I want to execute script before I shutdown my machine that writes date to a file (so I do know what time I finish working).

How to do it?

share|improve this question
Possible duplicate – jcollado Nov 28 '11 at 17:48
possible duplicate of Execute a script upon logout/reboot/shutdown in Ubuntu – glenn jackman Jul 29 '14 at 16:59

What you are looking for is an init script: take a look at the Upstart How to.

You basically need to place your script in /etc/init.d and create a link in /etc/rc?.d, where "?" is the runlevel you want to execute it.

The name of the link determines the order and when (start or shutdown) it's executed. Links beginning with K are run on shutdown, beginning with S on startup. You should put the shutdown scripts in /etc/rc0.d/, and the startup scripts in /etc/rc2.d/. Restart scripts should be placed in /etc/rc6.d.

You could also execute a script when X ends or gnome logs you out, but I think init scripts are the cleanest solutions.

If going with the first option Debian based systems like Ubuntu provide the command line tool update-rc.d to assist with installing /etc/rc?.d scripts.

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.