208

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).

conny
  • 2,469

9 Answers9

218

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 an example - sshd:

$ 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 ;)

evandrix
  • 117
nayden
  • 2,756
28

To restart a service, you can use the launchctl kickstart command, together with the -k option. For example, to restart apache, you can use

sudo launchctl kickstart -k system/org.apache.httpd

This information is from the launchctl manual page:

 kickstart [-kp] service-target
          Instructs launchd to run the specified service immediately, regardless of its
          configured launch conditions.
      -k       If the service is already running, kill the running instance before
               restarting the service.
      [...]

jochen
  • 381
20

You could simply do

sudo launchctl stop com.openssh.sshd

If you don't known the full service name, you can do

sudo launchctl list

If you still don't find the service you expected, try to run without the root identity:

launchctl list

And you don't need to unload and load service.

7
sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist 10char
djdomi
  • 2,287
3

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.
eric.s
  • 439
3

Just in case if you are looking for launchctl reload, you can define shell function in your ~/.bashrc/.zshrc as I did:

function lctl {
    COMMAND=$1
    PLIST_FILE=$2
    if [ "$COMMAND" = "reload" ] && [ -n "$PLIST_FILE" ]
      then
        echo "reloading ${PLIST_FILE}.."
        launchctl unload ${PLIST_FILE}
        launchctl load ${PLIST_FILE}
      else
        echo "either command not specified or plist file is not defined"
    fi
}

Command execution looks like -> lctl reload <your-plist-name>.plist

0

For restarting, I think the easiest way is to kill the process. Assume you have configured keepalive, which most daemon processes do. pkill apache2 will do. Then the process will start again by itself.

0
sudo apachectl restart

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

Sven
  • 100,763
-1

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.

risyasin
  • 1,584