简体   繁体   中英

How to start python file as a service in OSX (I need both a start and a stop script)

I want to run a python program/file as a service on OSX. To do this properly I need a start and a stop script.

I have tried multiple solutions that I know work in LINUX, however none seem to fully work in OSX. I remember trying one that worked if the code was as simple as a 'Hello world', anything more complex and Python would crash.

The code I am trying to run works fine when I 'execute' the py script on the terminal.

I imagine this is something rather easy but I can't seem to get it to work.

PS: using % in the terminal does not work in this situation, I do really need a start and stop script.

Cheers, Thanks in advance.

I suggest you use Launchd for this. You write configuration files, place them in /System/Library/LaunchDaemons , then you can start and stop them with launchctl . It's not too hard to figure out.

You should read the manpages for launchctl , which is the program used to start and stop launch agents, and for launchd.plist , the configuration format. It's a long read, but not difficult ot understand. There are lots of options for timing and responding to events, but you'll only need the simplest. You'll find plenty of example scripts already in your system at the above path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM