Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

Suggestion 1

You could add your script to /etc/init.d and then link it to the appropriate runlevel as a K??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 …


shutdown -h now

which you could then execute using sudo

$ sudo /path/to/script/backup_then_shutdown
share|improve this answer
The script will need to be rewritten to work as an init script. See /etc/init.d/halt for an example. Also you need this in run levels 0 and 6 (ie softlinks in /etc/rc6.d and /etc/rc0.d ) and not in /etc/rc1.d which is single user mode and will probably never get used in this context. Also it will need to be called as an S script not a K script. Best example is to look how /etc/init.d/halt is run. Suggestion 2 will work but he already knows how to run it manually – Richard Holloway Mar 18 '10 at 13:48
@Convict Thanks. I'll give it a shot and see whether it works. – GeneQ Mar 22 '10 at 14:02

You could save the file as 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.

share|improve this answer
@Richard Holloway, Tried that. Doesn't seem to work on Knoppix Live. Thanks anyway. – GeneQ Mar 22 '10 at 14:04
Is /etc/ persistent. I mean can you make changes there which will be saved between restarts? Originally I posted about init scripts and run levels but removed it all from my answer when I read it was Knoppix. Now I see it is on a USB device so may be writable. I am happy to continue with this to get you a workable solution. – Richard Holloway Mar 22 '10 at 16:59

Your Answer


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.