I want to run a shell script upon system shutdown in Knoppix Live (which runs from a writable USB flash drive) so that I can backup some data and ftp it to a remote server. The script works fine but I'm not sure where to put it so that it executes when the system goes into shutdown.
Suggestion 1
You could add your script to
/etc/init.d
and then link it to the appropriate runlevel as aK??my_backup
. You will need your script to execute before the network interface is taken offline. On my Debian system, it looks like/etc/rc1.d
might be the correct runlevel to link into.My concern with this approach is if your script takes a long time to execute. eg FTP site is slow or not available. I'm not sure if the shutdown process will wait for your script to finish, or if your backup script will be killed because it is taking too long. I'll leave that as an experiment for you! :-)
Suggestion 2
You could write a little wrapper script to shutdown your system. Something along the lines of …
which you could then execute using
sudo
…You could save the file as backup.sh in your home directory within the area being backed up.
Call the script from the .bash_logout file in your home directory. So when the machine goes down, you get logged out, the script runs.