Start Python scripts with launchctl (OSX) -
i have simple test scrip want computer run every 60 seconds - time_test_script.py
. script saves .txt
file current time name , writes text onto file. file in /users/me/documents/python
dir.
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()
i have .plist
file – test.plist
in /library/launchagents
dir.
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>
if run script manually works fine, i.e. creates .txt
file in specified directory. however, when try initiate launchctl
terminal nothing happens.
$ launchctl load /library/launchagents/test.plist $ launchctl start com.test
what doing wrong?
if running script without using python scriptname.py
, script needs marked executable (chmod a+x scriptname.py
command line) , first line should tell system interpreter use, in case #!/usr/bin/python
.
for example:
sapphist:~ zoe$ cat >test.py print "hello world" sapphist:~ zoe$ ./test.py -bash: ./test.py: permission denied
with execute bit set:
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
with both interpreter , execute bit:
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$
Comments
Post a Comment