Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

How to run a command before the machine automatically shutdowns?

share|improve this question
    
What do you mean by "automatic shutdown"? Should it be run before the machine is shut down by a cron or before every shutdown? –  Lekensteyn Jun 29 '11 at 16:51
add comment

4 Answers

up vote 2 down vote accepted

How about to put a script in the correct runlevel? I think runlevel 6 is shutdown, and then the dir to play in is /etc/rc6.d/

share|improve this answer
    
This is more or less equivalent to what @geekosaur suggested. –  enzotib Jun 29 '11 at 20:07
    
Note that this gets really complicated, as you have to make the script /etc/rc6.d/K##something to order it properly. An upstart job would be simpler –  SpamapS Aug 5 '12 at 14:41
add comment

cron isn't the tool for this. You need to set up an upstart job which executes during shutdown, preferably declaring a dependency so that it runs before any services it requires shut down. man 5 init for the details on how to properly define a startup or shutdown task; I can't help beyond that without more information, as it depends on what you're trying to do and what other upstart jobs it depends on.

share|improve this answer
    
My question is : How to run a command automatically before the machine shutdown? –  waterloo2005 Jun 29 '11 at 17:48
    
I mount firefox *.profile directory with tmpfs in ram at startup . When I shutdown machine , I want to tar that profile directory to a file automatically to keep the content. –  waterloo2005 Jun 29 '11 at 17:52
1  
So, as man 5 init tells you, there are several ways to define an upstart job for this: it could be a script triggered start on shutdown, or more accurately start on stopping mountall. Or you could start on started mountall and stop on stopping mountall, and use pre-start to create, mount, and populate the ramdisk and post-stop to save and umount it. –  geekosaur Jun 29 '11 at 18:19
add comment

Try this one, replacing $HOME and $PROFILE with vapid values and save it to ~/.init with permission 644.

Before you do anything, make sure there is a line like

firefox /home/your_name/.mozilla/firefox/your_profile.default tmpfs size=128M,noauto,user,exec,uid=1000,gid=100 0 0

in /etc/fstab, where the mount option noauto prevents tmpfs from being auto-mounted, which could be changed to auto to your preference. I leave it there to keep the whole upstart job in integrity.

description     "automatic firefox profile ram caching"

start on started mountall
stop on stopping mountall
env HOME="/home/your_name"
env PROFILE="your_profile.default"

script
if test -z "$(mount | grep -F "${HOME}/.mozilla/firefox/${PROFILE}" )"
then
mount "${HOME}/.mozilla/firefox/${PROFILE}"
fi
end script


post-start script
cd "${HOME}/.mozilla/firefox"
if test -f packed.tar
then
    tar xpf packed.tar
    echo "$(date): firefox profile unpacked to ram" >> unpack.log
else
    tar xpf packed.tar.old
    echo "$(date): backup profile unpacked to ram" >> unpack.log
fi
touch "${PROFILE}/.unpacked"
end script 


pre-stop script
cd "${HOME}/.mozilla/firefox"
if test -f "${PROFILE}/.unpacked"
then
    tar --exclude '.unpacked' -cpf packed.tmp.tar "$PROFILE"
    mv packed.tar packed.tar.old
    mv packed.tmp.tar packed.tar
    rm "$PFORILE/.unpacked"
    echo "$(date) firefox profile packed from ram" >> pack.log
else
    echo ".unpacked is missing. pack current session data to newfile"
    tar -cpf packed-$(date +%Y%m%d%H%M).tar "$PROFILE"
fi
end script
share|improve this answer
add comment

As a simplified version of funicorn's answer.. an upstart job, either in /etc/init/foo.conf or as ~/.init/foo.conf :

start on starting rc RUNLEVEL=6
task
exec myscript

This will run, and block the entire shutdown until "myscript" exits.

That is all you need. Note that the user jobs (~/.init/foo.conf) are a very new feature and on my 12.04 system with ecryptfs /home do not work, so you may just have to put the job in /etc/init.

share|improve this answer
add comment

Your Answer

 
discard

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.