LaunchD Job失败了奇怪的原因:2

时间:2014-01-28 21:44:25

标签: python json mongodb launchd

我一直在尝试使用launchD来每24小时执行一次脚本,但由于奇怪的原因我一直收到错误:2;我理解这意味着找不到该文件,但我检查并仔细检查以确保程序参数中的文件路径是正确的。当我在终端中执行脚本时,脚本运行正常。关于为什么这不起作用的任何想法?

该脚本只是从AngelList中删除新添加的初创公司的信息,并通过电子邮件发送出去。

下面是我的launchD和Python脚本:

Python: 

#!/opt/local/bin/python2.6
import urllib
import json
import datetime
import ast
import time
import smtplib
from pymongo import MongoClient
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

... #code omitted 

def find_data(): 
   client = MongoClient()
   client = MongoClient('localhost', 27017)
   db = client.tiger_labs
   collection = db.new_startups
   existing_names = []
   for startup in collection.find(): 
      existing_names.append(startup.get('name')) # inserts all "already-found" startups to the database

   market_tags = ["mobile health", "health care", "health care information technology", "health and wellness", "personal health"] # lists out all tags

   ... # code omitted

   text += "</html>"
   if text != "<html></html>": # sends e-mail if not empty
       nathan = "testerisgood@gmail.com"
       tigerlabs = "****@tigerlabs.co, *****@tigerlabs.co"
       msg = MIMEMultipart('alternative')
       msg['Subject'] = "New Startups"
       msg['From'] = nathan
       msg['To'] = tigerlabs
       part1 = MIMEText(text, 'html')
       msg.attach(part1)
       server = smtplib.SMTP('smtp.gmail.com:587')  
       server.starttls()  
       server.login("testerisgood@gmail.com", "******")
       server.sendmail(nathan, tigerlabs, msg.as_string())
       server.quit()
       print "sent"
   else: # purely for testing reasons; wanted to see if launchD was executing
       server = smtplib.SMTP('smtp.gmail.com:587')  
       server.starttls()  
       server.login("testerisgood@gmail.com", "*********")
       server.sendmail("testerisgood@gmail.com", "*****@princeton.edu", "Hi it worked.")
       server.quit()
       print "not_sent"

find_data()

LaunchD:

<?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.tigerlabs.TigerLabs</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/nathanlam/Desktop/Programming_Labs/Coding/TigerLabs.py</string>
    </array>
    <key>StartInterval</key>
    <integer>86400</integer>
    <key>KeepAlive</key>
    <dict>
          <key>NetworkState</key>
          <true />
    </dict>
    <key>StandardErrorPath</key>
    <string>/tmp/test.stderr</string>
</dict>
</plist>

1 个答案:

答案 0 :(得分:0)

在我的bash脚本中,我确实使每条路径都是绝对的。在我的LaunchAgent中我也做了同样的事情! 在我的LaunchAgent中,我使用了〜而不是/ Users / xyz。最后一次调整就行了!