![Systemd-路径不是绝对路径-无法执行](https://linux22.com/image/1606925/Systemd-%E8%B7%AF%E5%BE%84%E4%B8%8D%E6%98%AF%E7%BB%9D%E5%AF%B9%E8%B7%AF%E5%BE%84-%E6%97%A0%E6%B3%95%E6%89%A7%E8%A1%8C.png)
我正在使用 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