[英]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.