简体   繁体   English

linux 启动/关闭时的日志脚本

[英]Log script at linux startup/shutdown

I'm learning to use shell script on linux (I'm working on CentOS 6.7) and file system too.我正在学习在 linux(我正在使用 CentOS 6.7)和文件系统上使用 shell 脚本。 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:我已将以下内容放在 /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).然后我在 rc0 和 rc6 中创建了 K 个符号链接用于关机和重启,并在 rc5(我的默认设置)中创建了 S 个符号链接。

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.重新启动几次尝试不同的事情后,我只有“[时间]系统启动”,甚至给rc0中的脚本一个K01优先级。

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.也许重启时的 Kill 信号不像我认为的那样有效,但我不确定。

What you are trying to do is writing a service .您要做的是编写service As such you should not create those symlinks manually, instead you should use the chkconfig command to add your shell script as a new service.因此,您不应手动创建这些符号链接,而应使用chkconfig命令将您的 shell 脚本添加为新服务。

There are special comment lines that you need to add at the top of your script for chkconfig to handle it correctly.您需要在脚本顶部添加特殊的注释行,以便chkconfig正确处理它。 Take a look at both the man pages for chkconfig and service .查看chkconfigservice的手册页。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM