脚本在终端中运行良好,但不是从启动时运行 [英] script runs fine in terminal but not from launchd

查看:125
本文介绍了脚本在终端中运行良好,但不是从启动时运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Python脚本在终端中运行良好,但是当我尝试将其设置为在特定时间每天运行一次并启动(使用称为Lingon的软件进行设置)时,我只是无法运行它.从我读过的所有内容中,最好从Shell脚本中调用Python脚本(我在Macbook上,运行Yosemite).所以,这就是我想要做的.当脚本要运行时,我得到的错误是:

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. From everything I've read, it's better to invoke the Python script from a shell script (I'm on a Macbook, running Yosemite). So, that's what I'm trying to do. The errors I get when the script is due to be run are:

未设置TERM环境变量.

TERM environment variable not set.

env:python3:没有这样的文件或目录

env: python3: No such file or directory

在这一点上,我很确定这是一个环境问题,但是无论我如何尝试,我都无法使其运行.顺便说一句,我可以通过以下方式使Shell脚本按计划运行:

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:

#!/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.

推荐答案

我已经尝试了所有提到的内容,特别感谢Padraic,但似乎没有任何效果.这很奇怪,因为该脚本从终端运行时运行完美,但从启动运行时却出现错误.从启动运行时,我能够摆脱错误,但是脚本无法从终端运行.很奇怪.但是,这就是我如何使它在终端中按期启动并按计划运行的方式.首先,我从此更改了shebang行:

I've tried all that has been mentioned, special thanks to Padraic, but nothing seems to work. 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:

#!/usr/bin/env python3

对此:

#!/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')

对此:

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

无论如何,现在都可以使用,但是我认为我遇到的问题可能与将Mac升级到优胜美地OS有关.关于优胜美地中可能存在的有关launchd/launchd.conf/launchctl的错误,有一些提及.好吧,我想相信过去4天里试图使它起作用的不是我...但是谁知道呢?

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. There's some mention regarding a possible bug in Yosemite concerning 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?

这篇关于脚本在终端中运行良好,但不是从启动时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆