0

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.

jpsfer
  • 594
  • 3
  • 7
  • 18
  • what LINUX solutions have you tried ? – PepperoniPizza Apr 22 '14 at 01:48
  • Can you elaborate what you're trying to do? Is it something that you want to execute every x seconds? – Jonathan Howard Apr 22 '14 at 03:14
  • I have py script that looks for changes in a mongodb Database and then applies some changes to the new entries. I don't want to have a shell running the program, I want to run it in background through a start script and then have another script to stop it. The solutions I have tried involve creating a pid file using the OS module. tried these for example: http://stackoverflow.com/questions/473620/how-do-you-create-a-daemon-in-python – jpsfer Apr 22 '14 at 10:44

1 Answers1

2

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.

Joe
  • 46,419
  • 33
  • 155
  • 245