Need help making a Python script starup on mac using Launch Agent and send output to a window
I've created a python script I wish to have run automatically when I log in on a mac, and need some help using a Launch Agent to do this. I used SO extensively getting things working up to this point, so I figured this was the place to ask, but I'm still fairly new to this, so I need some help.
Here's what I've got so far and what I still need:
I have a python script that I can run from the terminal. It periodically will output text to the terminal window whenever it does something important (uses the python 'print' command). It also creates a log of this elsewhere, but I also want the realtime update in a window that stays open.
What I now need is a Launch Agent to start this script automatically on login and still output this text. I've gotten a basic Launch Agent working, but it runs the script in the background and I no longer can see the output. Based on my research so far, it looks like I can use the "StandardOutPath" keyword in the Lauch Agent .plist to redirect this output? I don't yet understand stdout/stdin/stderr in these programs well enough to understand how all 开发者_JAVA百科of this works, but I was hoping to essentially redirect this output, perhaps to an xterm window or similar.
I couldn't find a good example of this type of thing anywhere, so there may be a better way to do it, and I'm just not searching for the right thing. Any help or explanation on how all this works would be much appreciated.
Updated: My working Launch Agent .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>
<string>com.name.WVCS-0.1</string>
<key>RunAtLoad</key>
<true/>
<key>ProgramArguments</key>
<array>
<string>osascript</string>
<string>/Path/to/StartupScript</string>
</array>
</dict>
</plist>
If you want the output to appear in a terminal, you can use Launch Agent to start an Apple Script which will open Terminal and then run your python script:
tell application "Terminal"
activate
do script "/usr/bin/python WVCS-0.1.py"
end tell
or to display the text in the applescript / calling window
tell application "Terminal"
activate
do shell script "/usr/bin/python WVCS-0.1.py"
end tell
And then change your plist so that
<array>
<string>osascript</string>
<string>/path/to/that/script</string>
</array>
Not super elegant, but I hope that helps!
精彩评论