Home:ALL Converter>Log script at linux startup/shutdown

Log script at linux startup/shutdown

Ask Time:2016-02-11T17:43:56         Author:Agustin Garcia

Json Formatter

I'm learning to use shell script on linux (I'm working on CentOS 6.7) and file system too. Now I'm creating a script that writes messages in a text file when the system is started up or shut down. I've placed the following in /etc/init.d:

#!/bin/sh
#
# This is a test that send messages to a log
LOGPATH=/home/user/documents
now=$(date +'%T')

start() {
  echo "[$now] System startup" >> $LOGPATH/test.log
}

stop() {
    echo "[$now] System shutdown" >> $LOGPATH/test.log
}

status() {
    echo "[$now] Hi, you're checking status" >> $LOGPATH/test.log
}

case "$1" in
  start)
      start
      ;;
  stop)
      stop
      ;;
  restart)
      $0 stop
      $0 start
      ;;
  status)
      status
      ;;
  *)
      ## If no parameters are given, print which are avaiable.
      echo "Usage: $0 {start|stop|status}"
      exit 1
      ;;
esac

Then I created K symblinks in rc0 and rc6 for shutdown and reboot and S symblinks in rc5 (my default).

It worked on startup, but it didn't on shutdown. After rebooting a few times trying different things, I only have "[time] System startup", even giving a K01 priority to the script in rc0.

Why is not working on shutdown? Maybe Kill signal on reboot doesn't work like I think it works, but I'm just not sure.

Author:Agustin Garcia,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35335660/log-script-at-linux-startup-shutdown
yy