Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

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 …

#!/bin/bash

/path/to/backup/script/backup_to_ftp
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 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.

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

 
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.