![](/img/trans.png)
[英]Running Python Script at Startup to log mouse and keyboard activity in Linux
[英]Log script at linux startup/shutdown
我正在学习在 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 个符号链接。
它在启动时有效,但在关机时无效。 重新启动几次尝试不同的事情后,我只有“[时间]系统启动”,甚至给rc0中的脚本一个K01优先级。
为什么在关机时不工作? 也许重启时的 Kill 信号不像我认为的那样有效,但我不确定。
您要做的是编写service
。 因此,您不应手动创建这些符号链接,而应使用chkconfig
命令将您的 shell 脚本添加为新服务。
您需要在脚本顶部添加特殊的注释行,以便chkconfig
正确处理它。 查看chkconfig
和service
的手册页。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.