在Mac上启动服务执行bash命令

时间:2019-10-17 15:37:32

标签: launchd

我正在尝试在Mac上启动服务。该服务需要执行bash脚本。

我有以下内容:

watcher.service

[Unit]
Description=Watcher service - used to copy error logs

[Service]
Type=simple
ExecStart=watcher.sh

我在同一目录中:

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
while :; do grep ERROR $ExampleLogFile > $ErrorLogFile; sleep 2; done

echo "done"

我从终端转到同一个目录,然后运行:

sudo launchctl start watcher

什么都没发生。

如果我从终端运行以下命令:

sudo watcher.sh

它按预期方式工作(即echo被打印到终端并将行复制到文件中。

我认为问题在于该服务中的ExecStart没有执行watcher.sh脚本。

问题

我如何获得执行bash脚本的服务?

0 个答案:

没有答案
相关问题