How to run a command before the machine automatically shuts down?

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


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.


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

/etc/rc6.d/