I have a small backup bash script that I wrote for my computer at work. I have copied the script into /etc/rc0.d/ and called it K01backup so it is executed before anything else upon shutdown. It backs up all the data from my computer (running Ubuntu 14.04LTS) and my working copies and a virtual machine located on a separate internal SSD to an external hard drive and adds the log output to files in each folders.Here is the script:#!/bin/bashLOG=/syncLogVMORIG=/media/SSDData/VMPROJORIG=/media/Data/ProjectsDESTROOT=/media/ExtData/BackupsLOGVM=${DESTROOT}/VM${LOG}LOGPROJ=${DESTROOT}/Projects${LOG}ALLORIG=/DESTALL=${DESTROOT}/AllLOGALL=${DESTROOT}/All${LOG}echo STARTED > ${LOGPROJ}date +%d.%m.%Y/%H:%M:%S >> ${LOGPROJ}rsync -avvx --progress --no-whole-file ${PROJORIG} ${DESTROOT} >> ${LOGPROJ}echo FINISHED >> ${LOGPROJ}date +%d.%m.%Y/%H:%M:%S >> ${LOGPROJ}echo STARTED > ${LOGVM}date +%d.%m.%Y/%H:%M:%S >> ${LOGVM}rsync -avvx --progress --no-whole-file  ${VMORIG} ${DESTROOT} >> ${LOGVM}echo FINISHED >> ${LOGVM}date +%d.%m.%Y/%H:%M:%S >> ${LOGVM}echo STARTED > ${LOGALL}date +%d.%m.%Y/%H:%M:%S >> ${LOGALL}rsync -avvx --progress --no-whole-file --exclude /media/* --exclude /indel/* ${ALLORIG} ${DESTALL} >> ${LOGALL}echo FINISHED >> ${LOGALL}date +%d.%m.%Y/%H:%M:%S >> ${LOGALL}Then I ran sudo chmod +x /etc/rc0.d/K01backup to make it executable.At first the script took roughly an hour to execute and it all worked well. But for a little while now, I can see in the log files that the script started (presumably started again) an hour after I left work and all the files were already up to date, so it only took about a minute to run.Does anybody know what I might have done wrong?
Shutdown script seems to be executed twice
bash;backup;shutdown;runlevel;rc
The README in that directory states that scripts in that directory are only called once on poweroff (and not on reboot). With a simple test program#!/bin/bashLOG=/root/backup.logdate >> $LOGecho $* >> $LOGI noticed  that one time the program was actually called twice, once without a parameter and once with the parameter 'stop'. I have however not been able to reproduce it.I would suggest to log the actual invocation parameters to the program as well and in the script test for $1 being stop. It is also more customary to put this program as backup in /etc/init.d and make a link from /etc/rc0.d/K01backup to that script, but that should not influence its operation in any way. Any tools managing such entries work with creating/deleting these links.Based on trying out this basic script the OP found that there was a backup file from editing the file: /etc/rc0.d/K01backup~ that got executed as well. Putting the backup file in /etc/init.d/ from the start, and making a link would have prevented this from occuring (independent of whether there would be a /etc/init.d/backup~ file or not).