0

我有一个非常简单的测试脚本,我希望我的计算机每 60 秒运行一次 - time_test_script.py. 该脚本只是将.txt当前时间的文件保存为名称并将一些文本写入文件。该文件在/Users/me/Documents/Python目录中。

import datetime
import os.path
path = '/Users/me/Desktop/test_dir'
name_of_file = '%s' %datetime.datetime.now()
completeName = os.path.join(path, name_of_file+".txt")
file1 = open(completeName, "w")
toFile = 'test'
file1.write(toFile)
file1.close()
print datetime.datetime.now()

我还有一个.plist文件——test.plist/Library/LaunchAgents目录中。

test.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.test</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/me/Documents/Python/time_test_script.py</string>
    </array>
    <key>StartInterval</key>
    <integer>60</integer>
</dict>
</plist>

如果我手动运行脚本,它工作正常,即.txt在指定目录中创建一个文件。但是,当我尝试launchctl从终端启动时,什么也没有发生。

 $ launchctl load /Library/LaunchAgents/test.plist 
 $ launchctl start com.test

我究竟做错了什么?

4

1 回答 1

-1

python scriptname.py如果您在不chmod a+x scriptname.py使用#!/usr/bin/python.

例如:

Sapphist:~ zoe$ cat >test.py
print "Hello World"
Sapphist:~ zoe$ ./test.py
-bash: ./test.py: Permission denied

仅设置执行位:

Sapphist:~ zoe$ cat >test.py
print "Hello World"

Sapphist:~ zoe$ chmod a+x test.py
Sapphist:~ zoe$ ./test.py
./test.py: line 1: print: command not found

使用解释器和执行位:

Sapphist:~ zoe$ cat >test.py
#!/usr/bin/python
print "Hello World!"

Sapphist:~ zoe$ chmod a+x test.py
Sapphist:~ zoe$ ./test.py
Hello World!
Sapphist:~ zoe$
于 2016-02-08T16:46:28.790 回答