Running Python in background on OS X

后端 未结 5 1884
广开言路
广开言路 2020-12-04 11:27

Is there any way to keep my Python script (with an endless \'while\' loop) running in the background on OS X? Also, for the same purpose, is there any way to have \"autorun\

相关标签:
5条回答
  • 2020-12-04 12:05

    Just run your Python script in the background using the shell in the usual way:

    python myscript.py &
    

    As for the autorun question, this would be a huge security hole if it was something that Mac OS X did by default, so, no, of course not. But you could easily write a script that implemented something like that on purpose: periodically look for a USB drive plugged in, and do something (even run a second script from the thumb drive) when it's plugged in.

    0 讨论(0)
  • 2020-12-04 12:07

    I tried launchctl and couldn't make it work in El Capitan and searched around little more and found this post

    TL;DR

    Use shebang line #!/usr/bin/env python or #!/path/to/python on your script

    chmod +x test.py
    nohup /path/to/test.py &
    ps ax | grep test.py
    
    0 讨论(0)
  • 2020-12-04 12:12

    You can use nohup https://linux.die.net/man/1/nohup

    $ nohup python <your_script.py> &
    

    This will run your process and append the output to a file nohup.out in the same directory. & will run the script in the background as explained by other answers.

    0 讨论(0)
  • 2020-12-04 12:15

    If you want to have the script running as a daemon process which starts automatically, you can use launchctl and a plist file.

    For example, Bob has a simple python script which writes the word 'foo' to a file every second in his home directory:

    #!/usr/bin/env python
    import os
    import time
    
    while True:
      os.system('echo " foo" >> /Users/bob/foostore.txt')
      time.sleep(1)
    

    To have it run as a daemon process, create a plist file, ~/Library/LaunchAgents/com.bobbob.osx.test.plist, with the contents:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
    <plist version="1.0">
      <dict>
        <key>Label</key>
        <string>com.bobbob.osx.test</string>
        <key>Program</key>
        <string>/Users/bob/pyfoo.py</string>
        <key>KeepAlive</key>
        <true/>
      </dict>
    </plist>
    

    Then use launchctl to load the plist from a terminal:

    launchctl load ~/Library/LaunchAgents/com.bobbob.osx.test.plist
    

    This will load that script and immediately run the program in the <string> element beneath <key>Program</key>. You can also specify arguments for the program using a <ProgramArguments> node with an array of <string> elements. For more information see the launchd.plist man page

    If you want to remove the script, you can use the unload command of launchctl:

    launchctl unload ~/Library/LaunchAgents/com.bobbob.osx.test.plist
    

    The Label used in the script can be anything, but it should be unique on your system, so Apple generally uses a reversed domain name.

    As for autorunning a script, I don't think there's any way to do that.

    0 讨论(0)
  • 2020-12-04 12:28

    See this question for ways to daemonizing python scripts on Unix like systems: Python Daemon Packaging Best Practices

    Of course you can always run the script in the background as mentioned by kindall if that is all you need.

    0 讨论(0)
提交回复
热议问题