16

In Linux, we have the command /etc/init.d/process_name status, this will give whether the process/daemon is running or stopped.

For Example In Ubuntu:

root@Ubu91032b-Bassu:~# /etc/init.d/ssh status  
 * sshd is running  
root@Ubu91032b-Bassu:~#

My question is, is there any command (like above) in Mac to check the status of a daemon/process?

Kara
  • 6,115
  • 16
  • 50
  • 57
user389547
  • 161
  • 1
  • 1
  • 4
  • 1
    wrong place to ask this question. goto superuser.com – jmj Aug 07 '10 at 12:26
  • 1
    What if I want to do this programmatically? None of the answers have solutions for that. EDIT: Nevermind, there is another question for that here: http://stackoverflow.com/questions/2518160/programmatically-check-if-a-process-is-running-on-mac – livingtech Feb 08 '12 at 15:32

3 Answers3

23

The documented “modern” way would, I believe, be to ask launchctl, the controlling tool for launchd, which Apple uses to replace init, inetd, crond and a bit more:

~> sudo launchctl list | grep ssh
41032   -   0x100502860.anonymous.sshd
-   0   com.openssh.sshd
Christopher Creutzig
  • 8,656
  • 35
  • 45
  • 7
    But, in launchctl command we cant get the status of a daemon. It will show the PID of the daemon, if it is running else it will just show the "-". – user389547 Aug 11 '10 at 04:54
  • 3
    So you get information on whether the daemon is running, and the second entry is supposed to give you some form of “status”, it certainly is 1 for two startup launchd things on my machine that are broken. What more of a status are you asking for? And besides, you can also get information such as the last exit code, try `sudo launchctl list com.apple.fseventsd` or something like that. – Christopher Creutzig Aug 11 '10 at 07:04
  • One can check with the following code returning non-zero if the "Remote Login" is unchecked--disabling SSH in Mavericks OSX 10.9 (see http://support.apple.com/kb/PH13759 for more): `sudo launchctl list com.openssh.sshd` – Scott Robert Schreckengaust May 05 '14 at 19:01
4

To toggle remote login use the "System Preferences" => "Sharing" => "Remote Login" via the user interface to enable SSH (see http://support.apple.com/kb/PH13759 for more).

Remote Login via SSH Disabled (Unchecked):

$ sudo launchctl list com.openssh.sshd
launchctl list returned unknown response

Remote Login via SSH Enabled (Checked):

$ sudo launchctl list com.openssh.sshd
{
    "Label" = "com.openssh.sshd";
    "LimitLoadToSessionType" = "System";
    "OnDemand" = true;
    "LastExitStatus" = 0;
    "TimeOut" = 30;
    "Program" = "/usr/libexec/sshd-keygen-wrapper";
    "StandardErrorPath" = "/dev/null";
    "ProgramArguments" = (
        "/usr/sbin/sshd";
        "-i";
    );
    "inetdCompatibility" = {
        "Wait" = false;
    };
    "Sockets" = {
        "Listeners" = (
            file-descriptor-object;
            file-descriptor-object;
        );
    };
};
2

Yes there is a way to do this within the launchd/launchctl paradigm:

sudo launchctl bslist

will give you output of all loaded launchd processes, with

A for active. It's running

I for inactive. It's not supposed to run. It should not run on it's own, and I hope you notice how my tone is not definitive. But it's not supposed to surprise you, I should mean off.

D for on demand. Not running now, but could be, since it could have started at any time.

Also, if you want a treelike structure, so you can see which process fathered what:

sudo launchctl bstree

You'll get

 A  com.apple.windowserver.active
    D  com.apple.DirectoryService.localonly
    com.apple.metadata.mds[46].subset.109 (Explicit Subset)/
        D  com.apple.Spotlight.ImporterWorker.89
        D  com.apple.Spotlight.ImporterWorker.i386.89
        A  com.apple.Spotlight.ImporterWorker.501
        D  com.apple.Spotlight.SyncScanWorker

Which is a tree of the processes and their states.

If you are anything like me, you'll be wanting to use some stuff from here because you might find some peculiar things when you look.

chiggsy
  • 8,153
  • 5
  • 33
  • 43
  • "ssh" does not show up in Mavericks OSX 10.9 with either of the [launchctl](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/launchctl.1.html) subcommands "bslist" or "bstree", it does with the "list" subcommand though... – Scott Robert Schreckengaust May 05 '14 at 19:04
  • @ScottRobertSchreckengaust sshd is the thing that would show up via sudo commands, since root sees things in the Background, or daemon level. `ssh` is agent process ( per user) that interfaces with the background daemon. – chiggsy May 07 '14 at 00:38
  • @ScottRobertSchreckengaust for example, on my system `launchctl list 0x7f9ad951e160.anonymous.ssh { "Label" = "0x7f9ad951e160.anonymous.ssh"; "LimitLoadToSessionType" = "Aqua"; "OnDemand" = true; "LastExitStatus" = 0; "PID" = 1771; "TimeOut" = 30; "Program" = "ssh"; };` – chiggsy May 07 '14 at 00:38
  • Yes, using the "list" subcommand gives you information on SSH sessions and also (with sudo) the background daemon processes. On my Mavericks OSX 10.9 the "bslist" and "bstree" show neither "ssh" nor "sshd" processes or daemon background services. – Scott Robert Schreckengaust May 11 '14 at 14:28
  • 9
    bslist and bstree are now unimplemented legacy commands it seems, so this answer no longer works at all on Sierra+ – Gus Nov 18 '16 at 15:18