188

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,259
  • 2
  • 16
  • 14
  • 1
    From the answers so far, is correct to assume there is no single launchctl command to *restart* a service? – JS. Aug 25 '16 at 20:09
  • 1
    @JS. According to the manual page, `kickstart` together with the `-k` option seems to do the trick. See my answer below ... – jochen Jan 03 '19 at 16:08

9 Answers9

199

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

Dave M
  • 4,494
  • 21
  • 30
  • 30
nayden
  • 2,536
  • 1
  • 15
  • 8
  • 12
    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
  • 10
    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
  • 2
    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
  • 1
    @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
  • It always seemed quite unintuitive to me that something could be "loaded" while at the same time "stopped". I guess it will make sense, eventually :) – conny Oct 28 '10 at 19:18
  • 2
    @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
  • 9
    Why no mention of `launchctl start – notbrain May 18 '15 at 17:18
  • I get `sudo: launchctl: command not found` in Sierra. – zoechi May 22 '17 at 09:16
21

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
  • 311
  • 2
  • 3
  • 1
    When I tried this my service had hung, it did not stop after a short wait, "kill" on the pid did not help, doing "kill -9" on the pid killed the process and let kickstart (that had been running in another console while I tried to kill the process) continue and start the service again. I did not wait for very long before starting to kill things manually, so waiting long enough might have been enough to solve the problem. – Samuel Åslund Jun 28 '19 at 12:06
14

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.

  • 1
    I tried this and now I can't use `ssh` from my Mac. Rebooting doesn't fix. Replacing `stop` with `start` also doesn't fix. – P i May 25 '17 at 08:38
  • 1
    I tried this and I still can ssh to my Mac! – Michael Oct 01 '17 at 21:57
  • @dmitriy-apollonin It does not work, you must use `sudo unload /System/Library/LaunchDaemons/ssh.plist` instead. – SebMa Aug 07 '18 at 09:21
  • 1
    The question is about restarting a service, but I don't think that `launchctl stop` does restart a service. – jochen Jan 03 '19 at 13:50
7

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

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
  • 429
  • 1
  • 3
  • 12
  • 2
    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
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

Daniele Santi
  • 2,479
  • 1
  • 25
  • 22
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.

  • Find the process `ps -ef | grep ` then restart this process with `sudo kill -HUP `. You can check that the pid has changed. I find this great when working with services that are ported directly from linux that do not show up in launchctl – MortenB Sep 16 '22 at 07:49
0
sudo apachectl restart

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

Sven
  • 97,248
  • 13
  • 177
  • 225
  • 1
    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
-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,564
  • 9
  • 16