我正在使用 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
日志观察器
# 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