• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 564
  • Last Modified:

Run scripts on shutdown

I am trying to run a script on shutdown. I need to copy some data to an nfs mounted file system prior to shutdown. I created a script (/etc/init.d/loginfo) with the correct permissions and links in /etc/rc*.d.
When I run the /etc/init.d/loginfo script via the command line it does what I want it to do. When I reboot the system the script does not execute.
Here is the script:
#!/bin/sh  
#
# Startup script for the
#
# chkconfig: 2345 98 10
# Source function library.
. /etc/rc.d/init.d/functions
case "$1" in
        start)
                echo -n "Starting info  services: "
                dt=`date '+%a %b %e %T'`                    
                echo "$dt system booted" >> /var/log/bootlog
        ;;
        stop)
                echo -n "Running Logging information: "
                dt=`date '+%a %b %e %T'`                                  
 echo "$dt system booted" >> /var/log/bootlog
        ;;
        stop)
                echo -n "Running Logging information: "
                dt=`date '+%a %b %e %T'`                                  
                echo "$dt system shutdown" >> /var/log/bootlog              
               # Run the linux_savelogs.sh script                        
                sh /home/audits/scripts/linux_savelogs.sh                  
               # This script copies the logs to /home/audits/..
               # Copies to /home/audits/...      
               # Compresses (GZIP) raw file                              
               TODAY=`date '+%y%m%d'`                                    
               NODENAME=`hostname`                                        
               OSNAME=`uname -s`                                          
               LOGFILE=/var/log/audit.log                                
               RAW_ARCHIVE="/home/audits/SWUNIX/$OSNAME/bmd/snare_raw/"  
               RAWFILE="${RAW_ARCHIVE}${TODAY}.snare.$NODENAME"          
               TXT_ARCHIVE="/home/audits/SWUNIX/$OSNAME/bmd/snare/"      
              TXTFILE="${TXT_ARCHIVE}${TODAY}.snaredistilled.$NODENAME"  
 cp $LOGFILE $RAWFILE
if [ -e $RAWFILE ]
then
 rm $LOGFILE
fi
if [ -e $TXTFILE ]
then
 rm $TXTFILE
fi
cat $RAWFILE | /home/audits/scripts/snare_distill.pl > $TXTFILE
chgrp uaudits $TXTFILE
chmod 640 $TXTFILE
chmod 640 $RAWFILE
gzip -q --force $RAWFILE
        ;;
        *)
                echo "Usage: loginfo{start|stop}"
                exit 1
        ;;
esac
0
lhaynes4
Asked:
lhaynes4
  • 2
2 Solutions
 
theruckCommented:
you need to find your shutdwon script and add this script before anything else is executed. the location and name of the script depends on your linux distribution
probably the shutdown script kill your script before it is finished...
0
 
wesly_chenCommented:
> /etc/init.d/loginfo
ln -s  /etc/init.d/loginfo  /etc/rc0.d/K00loginfo
ln -s /etc/init.d/loginfo  /etc/rc6.d/K00loginfo

You need to use
init 0
init 6
or
shutdown -r now
to reboot your linux box. "reboot" command will skip shutdown scripts.
0
 
theruckCommented:
or you can create an own shutdown script which would contain your script and at the end of the script will be the shutdown command
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now