Run scripts on shutdown

Posted on 2005-03-24
Medium Priority
Last Modified: 2013-12-15
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:
# Startup script for the
# chkconfig: 2345 98 10
# Source function library.
. /etc/rc.d/init.d/functions
case "$1" in
                echo -n "Starting info  services: "
                dt=`date '+%a %b %e %T'`                    
                echo "$dt system booted" >> /var/log/bootlog
                echo -n "Running Logging information: "
                dt=`date '+%a %b %e %T'`                                  
 echo "$dt system booted" >> /var/log/bootlog
                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'`                                    
               OSNAME=`uname -s`                                          
if [ -e $RAWFILE ]
if [ -e $TXTFILE ]
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
Question by:lhaynes4
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 14

Accepted Solution

theruck earned 452 total points
ID: 13626639
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...
LVL 38

Assisted Solution

wesly_chen earned 448 total points
ID: 13628657
> /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
shutdown -r now
to reboot your linux box. "reboot" command will skip shutdown scripts.
LVL 14

Expert Comment

ID: 13629998
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

Featured Post

Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month11 days, 9 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question