1

I'm trying to set an automatically program for getintraday.py in mac using launchctl.

1. Create run.sh

go to getintraday.py directory

cd /Users/yindeyong/Desktop/Django_Projects/pythonstock


vi run.sh

Following is 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

Then save and quite

 :wq
chmod 777 run.sh

2. Create plist document

go to ~/Library/LaunchAgents,create com.demo.plist

Following is create 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. Load command

    launchctl load -w com.demo.plist

But I get /Users/yindeyong/Desktop/Django_Projects/pythonstock/com.demo.plist: No such file or directory

William
  • 3,724
  • 9
  • 43
  • 76
  • At least the `Label` string must be specified without the `plist` extension. And are you sure that the plist file is located in `~/Library/LaunchAgents`? – vadian Dec 27 '18 at 17:31

1 Answers1

4

Since you didn't specify a path for the plist file, it assumed your current working directory (/Users/yindeyong/Desktop/Django_Projects/pythonstock). Try specifying the actual location of the plist file:

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

BTW, you should actually use a different name and label for your launch agent. The current name implies that it's produced by the owners of the domain demo.com, which I presume is not you (see: reverse domain name notation). If you don't have a domain name of your own to attribute the agent to, use something with a "local." prefix (e.g. "local.demo"). Also, the label should not include ".plist", that's a suffix that's added to the file name. Thus, you could use the label "local.demo" and name the file "local.demo.plist".

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151