views:

66

answers:

3

Hello fellow computer people :)

I have a shell script that I will use as a watchdog timer. It checks to see if my other 'main' program is running. If it is not, it restarts it.

The question is: How do I get this installed on a Mac? Is there a folder/plist file scenario somewhere where the OS will automatically and periodically call the script, ensuring that my program never goes that long without running? I would ideally like to test every minute, but every hour or even a couple of times a day would be satisfactory.

Thank you!

+2  A: 

I'm not a Mac user but there should be cron daemon. http://hints.macworld.com/article.php?story=2001020700163714

doc
+1  A: 

Crontab should do you nicely. Set your script to run every X minutes and cron will do the rest. If you prefer a GUI interface to your programs, try cronnix.

Stephen
crontab is not the Mac OS X "way". See gavinb's answer for the official answer.
JeremyP
@JeremyP I accept your comment but wish a clarification if you would. By not the Max OSX "way", do you mean that Launch Services incorporates Cron, and has definite benefits (over user friendliness, i.e. system benefits) over using Cron to schedule tasks? Or do you simply mean it is encouraged to use Launch Services over Cron?
Stephen
Launch Services can do everything cron can do and a lot more. The aim is to provide a unified framework for everything that needs to be launched without user intervention. It replaces inittab, rc.d, inetd, cron, at and standard Unix daemons and probably a load of other stuff for starting background tasks that has accumulated over the years as well. It's not easier to configure than cron but Apple alleges it works better.
JeremyP
It does work better. It provides declarative syntax for specifying types of jobs. It allows inversion of control, putting the OS in charge of spawning processes when they are called for, instead of each individual script. With cron, you can say "execute this process every X minutes". With launch agents, you can say declare a process for execution whenever a volume is mounted, or whenever a user logs in, or "this process should always be running". Mac OS then provides a unified framework for managing these services and takes care of the nitty gritty.
masonk
+4  A: 

The way to do this on Mac OS X is using Launch Services. It replaces the older system services such as init and crontab and provides a single, unified framework for managing system services.

In your case, you probably don't need a separate script - keeping an instance of your app running should be handled by the system. First you need to create .plist file that describes your daemon/script/application. You place it in one of the following locations, depending on the type of service:

  • ~/Library/LaunchAgents: Per-user agents provided by the user.
  • /Library/LaunchAgents: Per-user agents provided by the administrator.
  • /Library/LaunchDaemons: System wide daemons provided by the administrator.
  • /System/Library/LaunchAgents: Mac OS X Per-user agents.
  • /System/Library/LaunchDaemons: Mac OS X System wide daemons.

Once you have defined the service, you can use the launchctl command to control launchd. For example, you can list running services, start/stop services, and so on.

The full documentation is here:

gavinb