我正在使用Linux。我可以存取我的用戶的根目錄。
root@marais:~# pwd
/root
在這個目錄中我有以下內容:
root@marais:~# ls
example.log log_watcher.service log_watcher.sh
log_watcher.service
[Install]
WantedBy=multi-user.target
[Unit]
Description=Log Watcher Service
[Service]
ExecStart=log_watcher.sh
日誌觀察者.sh
# File to output to
ErrorLogFile="./error.log"
# File to read from
ExampleLogFile="./example.log"
if [ ! -e "$ErrorLogFile" ]; then # check if file exists
echo "Creating error file $ErrorLogFile"
touch $ErrorLogFile
fi
echo "copying error messages from $ExampleLogFile to $ErrorLogFile"
# copy from one file to the other
### Challenge 1 ###
# grep ERROR $ExampleLogFile > $ErrorLogFile
### Challenge 2 ###
# tail -f $ExampleLogFile | grep --line-buffered ERROR | tee $ErrorLogFile
while :; do grep ERROR $ExampleLogFile > $ErrorLogFile; sleep 2; done
echo "done"
我運行以下命令:
systemctl enable log_watcher
這似乎工作成功,因為我已經發送訊息說它設定了符號連結。
現在,當我嘗試啟動服務時:
systemctl start log_watcher
似乎什麼也沒發生,就是log_watcher.sh
沒有被呼叫。
所以當我得到狀態時:
systemctl status log_watcher
它說 不是log_watcher.sh
用絕對路徑呼叫的。
root@marais:~# systemctl status log_watcher
● log_watcher.service - Log Watcher Service
Loaded: error (Reason: Invalid argument)
Active: failed (Result: exit-code) since Fri 2019-10-18 10:49:32 CEST; 9min ago
Main PID: 2850 (code=exited, status=203/EXEC)
Oct 18 10:49:32 marais systemd[1]: Started Log Watcher Service.
Oct 18 10:49:32 marais systemd[1]: log_watcher.service: Main process exited, code=exited, status=203/EXEC
Oct 18 10:49:32 marais systemd[1]: log_watcher.service: Unit entered failed state.
Oct 18 10:49:32 marais systemd[1]: log_watcher.service: Failed with result 'exit-code'.
Oct 18 10:50:12 marais systemd[1]: [/root/log_watcher.service:8] Executable path is not absolute, ignoring: log_watcher.sh
Oct 18 10:50:12 marais systemd[1]: log_watcher.service: Service lacks both ExecStart= and ExecStop= setting. Refusing.
Warning: log_watcher.service changed on disk. Run 'systemctl daemon-reload' to reload units.
當我跑步時:
root@marais:~# readlink -f log_watcher.sh
/root/log_watcher.sh
它顯示我的絕對路徑是/root/
,這是我在中定義的路徑log_watcher.service
:
ExecStart=/root/log_watcher.sh
問題
請您告訴我如何讓該服務啟動並執行log_watcher.sh
.
謝謝
答案1
在 .service 檔案中,我需要/bin/bash
在腳本的路徑之前新增。
例如,對於 backup.service:
ExecStart=/bin/bash /root/log_watcher.sh