在linux启动/关闭时记录脚本

时间:2016-02-11 09:43:56

标签: linux shell centos symlink

我正在学习在linux上使用shell脚本(我在CentOS 6.7上工作)和文件系统。现在,我正在创建一个脚本,在系统启动或关闭时将消息写入文本文件中。我已将以下内容放在/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

然后我在rc0和rc6中创建了K symblinks用于关闭和重启以及rc5中的S symblinks(我的默认值)。

它在启动时起作用,但它没有关机。重新启动几次尝试不同的事情后,我只有#34; [时间]系统启动",甚至给rc0中的脚本提供K01优先级。

为什么不关机?也许在重新启动时杀死信号并不像我认为的那样有效,但我还不确定。

1 个答案:

答案 0 :(得分:1)

您要做的是撰写service。因此,您不应手动创建这些符号链接,而应使用chkconfig命令将shell脚本添加为新服务。

您需要在脚本顶部添加一些特殊注释行,以便chkconfig正确处理它。请查看chkconfigservice的手册页。

相关问题