【问题标题】:Log script at linux startup/shutdownlinux启动/关机时的日志脚本
【发布时间】:2016-05-22 00:17:54
【问题描述】:

我正在学习在 linux(我正在使用 CentOS 6.7)和文件系统上使用 shell 脚本。现在我正在创建一个脚本,在系统启动或关闭时将消息写入文本文件。我在 /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 个符号链接用于关机和重启,并在 rc5 中创建了 S 个符号链接(我的默认值)。

它在启动时工作,但在关机时没有。重启几次尝试不同的东西后,我只有“[time] System startup”,甚至给rc0中的脚本一个K01优先级。

为什么不工作在关机?也许重启时的终止信号不像我认为的那样有效,但我不确定。

【问题讨论】:

  • 真的不是编程问题,是吗?

标签: linux shell centos symlink


【解决方案1】:

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

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

【讨论】:

  • 是的,我知道这是一项服务,但我想试试是否可以手动完成,现在我确定了。但是,它仍然无法正常工作。我应该给它更高的 K 优先级吗?
  • 您是否按照chkconfig 的手册页所述在脚本中添加了特殊的 cmets 后执行了chkconfig --add
  • 已解决:我手动尝试后按照说明进行操作,但仍然无法正常工作。解决方案是在启动时添加锁定文件并在关闭时将其删除。
猜你喜欢
  • 2017-08-04
  • 1970-01-01
  • 1970-01-01
  • 2020-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多