How can I run a command before the machine automatically shuts down?

  • 1
    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

How about to put a script in the correct runlevel? I think runlevel 6 is shutdown, and then the directory to play in is

|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
  • Runlevel 6 is reboot, not shutdown. Runlevel 0 is system halt (aka shutdown), but at that runlevel, no services (e. g. cron) will be running. – DopeGhoti Mar 10 '17 at 23:14

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.

|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

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"

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

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

pre-stop script
cd "${HOME}/.mozilla/firefox"
if test -f "${PROFILE}/.unpacked"
    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
    echo ".unpacked is missing. pack current session data to newfile"
    tar -cpf packed-$(date +%Y%m%d%H%M).tar "$PROFILE"
end script
|improve this answer|||||

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
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.

|improve this answer|||||

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.