1

我正在尝试使用launchctl在mac中为getintraday.py设置一个自动程序。

1.创建run.sh

转到 getintraday.py 目录

cd /Users/yindeyong/Desktop/Django_Projects/pythonstock


vi run.sh

以下是run.sh:

#!/bin/sh

# record start time
echo `date` >> /Users/yindeyong/Desktop/Django_Projects/pythonstock/log &&

# go to getintraday.py directory

cd /Users/yindeyong/Desktop/Django_Projects/pythonstock &&

# Execute python scripts according to my virtual environment
/Users/yindeyong/Desktop/Django_Projects/envs/stockenv/bin/python3.6 getintraday.py
# Execute done
echo 'finish' >> /Users/demo/log/Users/yindeyong/Desktop/Django_Projects/pythonstock/log

然后保存并相当

 :wq
chmod 777 run.sh

2.创建plist文档

去~/Library/LaunchAgents,创建com.demo.plist

以下是创建 com.demo.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>
      <!-- Label -->
      <key>Label</key>
      <string>com.demo.plist</string>
      <!-- the script to Execute -->
      <key>ProgramArguments</key>
      <array>
        <string>/Users/yindeyong/Desktop/Django_Projects/pythonstock/run.sh</string>
      </array>
      <!-- the time to Execute  -->
      <key>StartCalendarInterval</key>
      <dict>
            <key>Minute</key>
            <integer>37</integer>
            <key>Hour</key>
            <integer>10</integer>
      </dict>
    <!-- Standard output file -->
    <key>StandardOutPath</key>
    <string>/Users/yindeyong/Desktop/Django_Projects/pythonstock/run.log</string>
    <!-- 
Standard error output file, error log -->
    <key>StandardErrorPath</key>
    <string>/Users/yindeyong/Desktop/Django_Projects/pythonstock/run.err</string>
    </dict>
    </plist>
  1. 加载命令

    launchctl 加载 -w com.demo.plist

但我得到/Users/yindeyong/Desktop/Django_Projects/pythonstock/com.demo.plist: No such file or directory

4

1 回答 1

2

由于您没有为 plist 文件指定路径,它假定您当前的工作目录 (/Users/yindeyong/Desktop/Django_Projects/pythonstock)。尝试指定 plist 文件的实际位置:

launchctl load -w ~/Library/LaunchAgents/com.demo.plist

顺便说一句,您实际上应该为您的启动代理使用不同的名称和标签。当前名称暗示它是由域 demo.com 的所有者制作的,我认为它不是您(请参阅:反向域名表示法)。如果您没有自己的域名来归属代理,请使用带有“本地”的名称。前缀(例如“local.demo”)。此外,标签不应包含“.plist”,这是添加到文件名的后缀。因此,您可以使用标签“local.demo”并将文件命名为“local.demo.plist”。

于 2018-12-28T05:09:39.577 回答