简体   繁体   English

脚本在终端中运行良好,但不是从启动时运行

[英]script runs fine in terminal but not from launchd

My Python script runs fine in the terminal but when I try to set it up to run once a day at a certain time with launchd (set up using software called Lingon), I just can't get it to run. 我的Python脚本在终端中运行良好,但是当我尝试将其设置为每天在特定时间运行一次(使用称为Lingon的软件进行设置)时,我只是无法运行它。 From everything I've read, it's better to invoke the Python script from a shell script (I'm on a Macbook, running Yosemite). 从我读过的所有内容中,最好从Shell脚本中调用Python脚本(我在Macbook上运行Yosemite)。 So, that's what I'm trying to do. 所以,这就是我想要做的。 The errors I get when the script is due to be run are: 当脚本要运行时,我得到的错误是:

TERM environment variable not set. TERM环境变量未设置。

env: python3: No such file or directory env:python3:没有这样的文件或目录

At this point, I'm pretty sure it's an environment issue but no matter what I try, I just can't get it to run. 在这一点上,我很确定这是一个环境问题,但是无论我如何尝试,我都无法使其运行。 By the way, I can get the shell script to run on the schedule this way: 顺便说一句,我可以通过以下方式使Shell脚本按计划运行:

#!/bin/bash
echo "hello world."

The problem comes when I try to run this: 当我尝试运行此问题时:

#!/bin/bash
/Users/jeff/Documents/scripts/my_script.py

Also, even though I've been working with computers for a long time, I'm still pretty ignorant about a lot of things, so please tell me how to fix this like I'm a newbie. 另外,即使我已经使用计算机很长时间了,我还是对很多事情一无所知,所以请告诉我如何解决这个问题,就像我是新手一样。

Based on the article here you need to create a .plist for launchd something like the following: 根据此处的文章您需要为启动以下内容创建一个.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>
    <!-- The label should be the same as the filename without the extension -->
    <string>org.yourusername.my_script-test</string>
    <!-- Specify how to run your program here -->
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/python3</string>
        <string>/Users/jeff/Documents/scripts/my_script.py</string>
    </array>
    <!-- Run every hour -->
    <key>StartInterval</key>
    <integer>3600</integer><!-- seconds -->
</dict>
</plist>

Then: 然后:

$ launchctl load ~/Library/org.yourusername.my_script-test.plist
$ launchctl start org.yourusername.my_script-test

An article here covers environment variables 这里的文章介绍了环境变量

I've tried all that has been mentioned, special thanks to Padraic, but nothing seems to work. 我已经尝试了所有提及的内容,特别感谢Padraic,但似乎没有任何效果。 It's strange because the script runs perfect when run from the terminal but comes up with errors when run from launchd. 这很奇怪,因为该脚本从终端运行时运行完美,但从启动运行时却出现错误。 I was able to get rid of the errors when run from launchd but then the script would not run from the terminal. 从启动运行时,我能够摆脱错误,但是脚本无法从终端运行。 Very strange. 很奇怪。 But here's how I did get it to run in both the terminal and on schedule from launchd. 但是,这就是我如何使它在终端中按期运行并按期启动。 First, I changed the shebang line from this: 首先,我从此更改了shebang行:

#!/usr/bin/env python3

to this: 对此:

#!/usr/bin/env /Library/Frameworks/Python.framework/Versions/3.4/bin/python3.4

I then had to specify in the rest of the script the full path to files, for example, from this: 然后,我不得不在脚本的其余部分中指定文件的完整路径,例如,从此路径:

log = open('log_directory/my_log.log', 'a')

to this: 对此:

log = open('/Users/jeff/documents/my_script_documents/python/development/log_directory/my_log.log', 'a')

In any event, it all works now, but I believe the problem I've had may have something to do with having upgraded my Mac to the Yosemite OS. 无论如何,这一切现在都可以使用,但是我认为我遇到的问题可能与将Mac升级到优胜美地OS有关。 There's some mention regarding a possible bug in Yosemite concerning launchd/launchd.conf/launchctl. 关于优胜美地中可能存在的有关launchd / launchd.conf / launchctl的错误,有一些提及。 Well, I'd like to believe it was not me for the past 4 days trying to get this to work...but who knows? 好吧,我想相信过去4天里试图使它起作用的不是我...但是谁知道呢?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python脚本可在终端中使用,但不能在启动时使用 - Python script works in terminal but not with launchd Python 脚本从终端运行良好,但从 crontab 运行时崩溃 - Python script runs fine from terminal but crashes when run from crontab 脚本在终端上运行,但不是 cron - Script runs on terminal, but not as cron 已启动,可防止脚本更改桌面图片 - Launchd prevents script from changing desktop picture 为什么我的 py 脚本在终端中运行良好,但在使用 pyinstaller 编译时出现错误? - Why does my py script run fine in terminal but runs into errors when compiled with pyinstaller? 在 Raspberry 上从终端运行 Python 脚本失败,但在 IDE 中运行很好 - Running Python Script from Terminal on Raspberry fails but running in IDE is fine Python脚本在终端中运行,但不作为Apache运行 - Python script runs in terminal but not as Apache pycharm:表示未安装软件包,但服务器可以从终端正常运行,没有任何错误 - pycharm: says package not installed but server runs fine from terminal without any error Code fail with name is not defined 使用 pydev 运行的错误,但从终端运行良好 - Code fail with name is not defined error running with pydev but runs fine from terminal 命令在终端或bash脚本中运行正常,但在使用子流程模块的python脚本中运行不正常 - Command works fine from terminal or inside bash script but not from python script using subprocess module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM