Launchctl minimal working example with Python

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

I'd like to run a python script every minute using launchd. My plist file looks like this:

Labelcom.turtle.script.plistProgramArguments/usr/bin/python/Users/turtle/Desktop/turtle.py/Users/turtle/Desktop/data/data.txtStartInterval60

This plist file looks good, as I get the following:

plutil -lint com.turtle.script.plist com.turtle.script.plist: OK

The script works when I run it from the command line:

/usr/bin/python /Users/turtle/Desktop/turtle.py /Users/turtle/Desktop/data/data.txt

I load this plist via:

   launchctl load -w -F com.turtle.script.plist

I've alse tried:

sudo launchctl load -w -F com.turtle.script.plist

I load this job and the python script should write out a file to disk. However no file is ever produced. I examine the job with:

sudo launchctl list | grep com.turtle.script.plist

The output is:

- 1 com.turtle.script.plist

Can anyone help trouble-shoot the problem?

回答1:

It sounds like there's some environment dependence inside the script -- essentially, it assumes something about the environment it's running in that's correct when you run it by hand, but not when launchd runs it. Without knowing anything about the script, it's hard to point at what this might be, but I can suggest a few things to look at:

  • sudo launchctl isn't a more powerful version of launchctl, it does something significantly different. You need to figure out which one you want, and use it.

    When you run launchctl as a normal user (e.g. launchctl load), it interacts with your user instance of launchd to manage Launch Agents -- items that run in your user session, under your user identity.

    When you run launchctl as root (e.g. sudo launchctl load), it interacts with the system instance of launchd to manage Launch Daemons -- items that run in system context, as root.

    You'll have to decide which is appropriate, based on what this script does.

  • Check system.log (you can use the Console utility to view it, or tail -f /var/log/system.log) and see if it includes anything to indicate why the script is failing.

  • Add entries to the launchd .plist to record the script's output, and see if that includes any error messages or other indications of what's going wrong:

    StandardOutPath/tmp/turtle.outStandardErrorPath/tmp/turtle.err

    It may help to edit the script to add debugging output, so you can tell more about how it's working (/not working).

  • Does the script depend on having a particular working directory and/or environment variables? If so, add appropriate WorkingDirectory and/or EnvironmentVariables items to the .plist.



回答2:

Try to write to /tmp which can be written by any user. i.e. change /Users/turtle/Desktop/data/data.txt to /tmp/my_data.txt if that is your output file.



回答3:

Your .plist file in ~/Library/LaunchAgents:

Labellocal.tf.check_upProgram/Users/tf/.bin/check_up.pyRunAtLoadStandardErrorPath/tmp/local.tf.check_up.stderrStandardOutPath/tmp/local.tf.check_up.stdoutStartInterval60WorkingDirectory/tmp/

Your script /Users/tf/.bin/check_up.py:

#!/opt/local/bin/python  f = open('/Users/tf/Desktop/test.txt', 'a') f.write('hello again 4\n') f.close()

Please note that I use python from MacPorts, which lives in /opt/local/bin/. If you use a different python interpreter, replace the above line with whatever $ which python returns.

Make sure that your script is executable and only you have write access:

$ chmod 755 ~/.bin/check_up.py

To test the script: Run it and see that it works as it should:

$ ~/.bin/check_up.py

Load the LaunchAgent:

$ launchctl load ~/Library/LaunchAgents/local.tf.check_up.plist


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!