I am running ubuntu 11.10 in a virtual machine (VirtualBox) to learn more about development in linux. I am using a git repository to save my work and have written a script to bundle my work up and save it to the shared folder for use while the virtual machine is not running.

I would like to automatically run this script before shutdown so that my work is always available if the vm is off (currently I have to manually run the script).

I don't know if upstart is the best way to accomplish this, but this is the config that I wrote as a test:

description     "test script to run at shutdown"

start on runlevel [056]


touch /media/sf_LinuxEducation/start
sleep 15
touch /media/sf_LinuxEducation/start-long
end script

pre-start script
touch /media/sf_LinuxEducation/pre-start
sleep 15
touch /media/sf_LinuxEducation/pre-start-long
end script

post-start script
touch /media/sf_LinuxEducation/post-start
sleep 15
touch /media/sf_LinuxEducation/post-start-long
end script

pre-stop script
touch /media/sf_LinuxEducation/pre-stop
sleep 15
touch /media/sf_LinuxEducation/pre-stop-long
end script

post-stop script
touch /media/sf_LinuxEducation/post-stop
sleep 15
touch /media/sf_LinuxEducation/post-stop-long
end script

The result is that only one touch is accomplished (the first touch in pre-start). What do I need to change to see one of the touches after the sleep to work? Or Is there an easier way to get this accomplished?

Thanks in advance.


The Upstart Intro, Cookbook and Best Practices has a great number of code snippets to use in creating upstart tasks and jobs.

The shutdown process section of the cookbook says that /etc/init/rc.conf will be run and call /etc/init.d/rc. In turn this will eventually call /etc/init.d/sendsigs. So if you start on starting rc then your task will be executed before rc (and the sigterms that would normally have the process shut down).

file: /etc/init/test.conf

description "test script to run at shutdown"

start on starting rc
exec /etc/init/test.sh

file: /etc/init/test.sh

touch /media/sf_LinuxEducation/start
sleep 15
touch /media/sf_LinuxEducation/start-long
| improve this answer | |
  • 1
    Added a concrete example to this question, but it doesn't work (only the first touch is run on shutdown) even though the documentation in Create an Event Alias says that it should. Please look at it an see if I made an error. – Tiris Nov 12 '11 at 2:31
  • Tiris, this should work. I'd be interested in seeing your /var/log/syslog after the reboot, particularly to see if test's process was forcibly killed by upstart. – SpamapS Nov 22 '11 at 6:44
  • 1
    The latest edit of this answer does work. Check the revision history to see the problem child. I guess I should have commented that I made an edit(or is it more proper to remove the comment and add a new one?Is there a stackexchange etiquette?). I would be interested in knowing why the problem child doesn't work (since the documentation says it should). – Tiris Nov 22 '11 at 22:11
  • What exactly does the test.conf file mean? Do I make a file called test.conf, or do I add this to rc.conf? – heneryville Feb 28 '13 at 5:54
  • @heneryville test.conf is just a random filename. You could just as easily use whatEverYouWant.conf. Your comment also shows that you didn't read the "Cookbook" mentioned in the answer. If you look in section 4.2 the Job Configuration File is more thoroughly explained. – Tiris Mar 2 '13 at 6:50

I think this cannot be done via upstart, as the /etc/init.d/sendsigs script, which is invoked by upstart when halting/restarting, kills all processes (killall5 -9) within 10 seconds, and even if that does not succeed, it goes for unmounting everything and shutdown.

The best way would be to use the rusty /etc/init.d style scripts.

Example: /etc/init.d/shutdown_job

#! /bin/sh
# Provides:          shutdown_job
# Required-Start:    
# Required-Stop:     sendsigs
# Default-Start:
# Default-Stop:      0 6
# Short-Description: bla
# Description: 


. /lib/lsb/init-functions

do_stop () {
    date > /root/s.log
    sleep 20
    date >> /root/s.log

case "$1" in
    # No-op
    echo "Error: argument '$1' not supported" >&2
    exit 3
    echo "Usage: $0 start|stop" >&2
    exit 3


Then activate the script

sudo update-rc.d shutdown_job start 19 0 6 .

This will put the script before the sendsigs script on runlevels 0 a 6 (shutdown, reboot). This sample script will record the date, then sleep for 20 secs, then record the date again to /root/s.log.)

More info:

| improve this answer | |
  • I am not at all familiar with doing this sort of thing. Is there a good guide on getting this to work? Please edit with more info. – Tiris Nov 12 '11 at 2:58
  • I tried this out and it doesn't work (well not exactly). I added a touch /media/sf_LinuxEducation/start and touch /media/sf_LinuxEducation/start-long right next to the date commands. The dates are logged, but the touches are not touched. This makes me wonder if that drive is unmounted at the time the script is run. I think it may have something to do with the NN argument, though I am not sure really how this argument works (the man page explains it in a very mind bending way). How do you know what number (or pair of numbers) should be set in this argument? – Tiris Nov 20 '11 at 22:39
  • Scripts get executed as per their numbers as seen when you list the /etc/rc6.d directory. So the 19 shutdown script should have been executed before any unmounting is done (>31). But why not try your hypothesis and touch a file in /root instead? – arrange Nov 20 '11 at 23:33
  • Looking in the dir /etc/rc6.d reveals that the the shared folders are being mounted/unmounted with the K70vboxadd script. Would changing the NN argument (for my script) to 71 get my script to run before the unmount of VB shared folders? I will try it later today. – Tiris Nov 22 '11 at 22:16
  • Well that didn't work. Update: I added a touch /root/start before the sleep 20 command and no file is created. I don't even know where to begin to find out why. The dates are happily added to the /root/s.log file why can I not touch a file? – Tiris Nov 22 '11 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.