我将plist文件保存到
/Library/LaunchDaemons/local.WiFiDaemon.plist
以下是代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Disabled</key>
<false/>
<key>GroupName</key>
<string>staff</string>
<key>InitGroups</key>
<true/>
<key>Label</key>
<string>local.job</string>
<key>ProgramArguments</key>
<array>
<string>python</string>
<string>/Library/Application Support/PythonDaemons/PythonTest.py</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>StandardErrorPath</key>
<string>/tmp/local.job.err</string>
<key>StandardOutPath</key>
<string>/tmp/local.job.out</string>
<key>UserName</key>
<string>myuser</string>
</dict>
该文件应该执行一个python脚本,它只是ping谷歌并将结果保存到文本文件中:
/Library/Application Support/PythonDaemons/Ping_log.txt
问题是python脚本给出了一个错误,说它没有文件的权限。当我从命令行运行脚本时,一切正常,没有任何特殊权限。我在这里错过了什么?
这是python脚本:
import subprocess, datetime, time
host = "www.google.com"
ping = subprocess.Popen(
["ping", "-c", "4", host],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE)
out, error = ping.communicate()
with open('PingOut.txt', 'w') as outFile:
outFile.write(out)
temp = out.split('\n')
parsed = temp[len(temp)-2].split('/')
min = parsed[len(parsed)-4].split(' ')[2]
avg = parsed[len(parsed)-3]
max = parsed[len(parsed)-2]
print "min: " + str(min) + "avg: " + str(avg) + "max: " + str(max)
with open('Ping_log.txt', 'a') as f:
f.write(str(datetime.datetime.now()) + ',' + str(min) + ',' + str(avg) + ',' + str(max))
PythonDaemons文件夹的权限是“drw-rw-rw - ”。
答案 0 :(得分:2)
提供日志文件的完整路径名:
with open('/Library/Application Support/PythonDaemons/Ping_log.txt', 'a') as f:
或者在plist中设置工作目录:
<key>WorkingDirectory</key>
<string>/Library/Application Support/PythonDaemons</string>
否则,它默认为您没有写入权限的系统目录。