Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How do I restart, say for example my httpd or afpd, running any Mac OS X >= 10.5 (Leopard-), without having to use the GUI and go to System Preferences -> Sharing and unchecking/checking "Web Sharing"?

I'm looking for the canonical equivalent to Debian's invoke-rc.d apache2 restart.

EDIT: The question is about launchd controlled services in general, not specifically Apache (-which was simply an example).

share|improve this question

5 Answers 5

up vote 48 down vote accepted

Hi launchctl(8) is your friend. Just keep in mind that some of the services (sshd for example) are disabled in the configuration file so you will need to use the '-w'switch when loading them. Here is a sshd example:

   $sudo launchctl load -w /System/Library/LaunchDaemons/ssh.plist 

You can stop the service using the 'unload' subcommand.

   $sudo launchctl unload  /System/Library/LaunchDaemons/ssh.plist 

To list the services, as you might have already guessed use the 'list' subcommand ;)

have fun, n

share|improve this answer
4  
In addition, launchctl list does not complain but does not show everything, sudo launchctl list is needed for that. –  conny Oct 26 '10 at 16:17
4  
It's not that it doesn't show everything, it shows a completely different list. When you run launchctl as a normal user, it shows/manages LaunchAgents running in your user session; run as root, it shows/manages the system-wide LaunchDaemons. –  Gordon Davisson Oct 26 '10 at 16:32
    
I can see "com.openssh.sshd" in that list, so I guess that config is "loaded". Now, how come for example sudo launchctl start com.openssh.sshd doesn't do anything, not evan fail with an error message, even less so actually launch anything? –  conny Oct 26 '10 at 16:39
    
@conny: you need to load the service first before starting it, this is what the load subcommand do. Contrary to the what you may one expect, one cannot start a service that hasn't been loaded yet. Once a service is loaded one can use 'stop' and 'start' subcommands to stop/start the service. To make sure the service is no longer running use the 'remove' command ('unload' would do that too) -- this will unload the service and any future 'launchctl start' commands will silently fail. Hope that helps. –  nayden Oct 26 '10 at 22:18
1  
@conny It might be confusing in the general case, however when you are writing a service and you suspect for example that something nasty is happening on startup, you can load it once and the use 'stop'/'start' to debug the issue. –  nayden Oct 28 '10 at 21:08

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

share|improve this answer
sudo apachectl restart

Works with other OSses as well as it is part of Apache.

share|improve this answer
    
Even though the topic doesn't mention apache in particular, I should have anticipated this interpretation.... I'm sorry, but that was not what I was looking for :) –  conny Oct 26 '10 at 13:20
    
Ok, I catched the httpd part, for which using apachectl is the easier variant :) –  Sven Oct 26 '10 at 14:30

You are looking for launchctl.

SYNOPSIS
 launchctl [subcommand [arguments ...]]

DESCRIPTION
 launchctl interfaces with launchd to load, unload daemons/agents and gen-
 erally control launchd.  launchctl supports taking subcommands on the
 command line, interactively or even redirected from standard input.
 These commands can be stored in $HOME/.launchd.conf or /etc/launchd.conf
 to be read at the time launchd starts.
share|improve this answer
    
But reading the man-page does not tell me what the equivalent of either invoke-rc.d my-little-daemon restart is ... It refers to "job_label" but does not tell me how to list the "job_label"s available. –  conny Oct 26 '10 at 13:51
    
I think nayden's response has got this covered. –  eric.s Oct 28 '10 at 13:29

There is a small & useful app for this named Lingon. Lingon freeware edition is here sometimes restarting a service can be tricky.

syslog -w

reading helps though.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.