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” python script on a USB drive?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
To have it run as a daemon process, create a plist file,
~/Library/LaunchAgents/com.bobbob.osx.test.plist, with the contents:Then use
launchctlto load the plist from a terminal: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 pageIf you want to remove the script, you can use the unload command of
launchctl: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.