How to view status of service (e.g. whether it's running) in a format similar to Linux `systemctl` using macOS `launchctl`?
I want to view the status of a service in macOS using launchctl
Hi,
to view the status, use print: launchctl print system/org.apache.httpd
Syntax is: launchctl print <domain>/<item>
Domain can be:
`system/<service>`
`user/<UID>`
`pid/<PID>`
`session/<asid>`
Various combinations and examples below:
Use: launchctl list
to list all services. Simple listing of known services, not detailed.
Sample output:
$ launchctl list
PID Status Label
659 0 com.apple.trustd.agent
- 0 com.apple.MailServiceAgent
- 0 com.apple.mdworker.mail
99270 0 com.apple.mdworker.shared.0E000000-0000-0000-0000-000000000000
- 0 com.apple.mdworker.shared.04000000-0000-0000-0000-000000000000
- 0 com.apple.appkit.xpc.ColorSampler
652 0 com.apple.cfprefsd.xpc.agent
- 0 com.apple.coreimportd
770 0 com.apple.TrustedPeersHelper
- 0 com.apple.SafariHistoryServiceAgent
1236 0 com.apple.progressd
3111 0 com.apple.cloudphotod
758 0 com.apple.Finder
693 0 com.apple.homed
- 78 com.ajsoft.a.mail
- The first column is the process PID
- The second column is a return code
- The 3rd column is process label
Itens with -
on the first column are NOT running
Itens with -
and not 0
on the second column were executed at least once, but are now either sleeping or stopped, example: - 78 com.ajsoft.a.mail
-
Detailed list of all services under the system domain: [all, including enabled and disabled ones]
-
sudo launchctl print system/
-
-
To get detailed list only of the DISABLED System services:
-
sudo launchctl print-disabled system/
-
-
To get ALL information about a specific system service
-
launchctl print system/com.vix.cron
-
sample output:
$ launchctl print system/com.vix.cron
com.vix.cron = {
active count = 0
copy count = 0
one shot = 0
path = /System/Library/LaunchDaemons/com.vix.cron.plist
state = waiting
program = /usr/sbin/cron
arguments = {
/usr/sbin/cron
}
default environment = {
PATH => /usr/bin:/bin:/usr/sbin:/sbin
}
environment = {
XPC_SERVICE_NAME => com.vix.cron
}
domain = com.apple.xpc.launchd.domain.system
domain umask = 22
minimum runtime = 10
exit timeout = 5
runs = 0
successive crashes = 0
last exit code = (never exited)
-
To print all services under the domain of the user with UID 501
launchctl print user/501
-
Get all information using the service or application PID (returned by the command 'launchctl list`)
-
launchctl print pid/758
-
sample output:
com.apple.xpc.launchd.domain.pid.Finder.758 = {
type = process
handle = 758
active count = 91
on-demand count = 1
service count = 90
active service count = 2
activity ratio = 0.02
originator = /System/Library/CoreServices/Finder.app
creator = Finder.758
creator euid = 503
uniqueid = 758
external activation count = 0
security context = {
uid = 503
asid = 100008
}
bringup time = 20 ms
death port = 0x52a63
in-progress bootstraps = 0
pended requests = 0
pending requests = {
}
subdomains = {
}
pending attachments = {
}
environment = {
PATH => /usr/bin:/bin:/usr/sbin:/sbin
SSH_AUTH_SOCK => /private/tmp/com.apple.launchd.Me9t1KNNjL/Listeners
TMPDIR => /var/folders/km/w6p6dvwx6qb16nzxdwy5m5080000gq/T/
}
services = {
0 - com.apple.amp.devicesui
0 - com.apple.security.pboxd
0 - com.apple.coremedia.videodecoder
0 - com.apple.SafariServices
0 - com.apple.MediaLibraryService
0 - com.apple.coreservices.SharePointManagementService
0 - com.apple.managedclient.pds.Exchange
0 - com.apple.SystemExtensionsMDM
0 - com.apple.LookupViewService
- To get information using the session id
-
launchctl print session/100008
-
sample output:
com.apple.xpc.launchd.domain.session.100008 = {
type = session
handle = 100008
active count = 1
on-demand count = 0
creator = launchctl.42668
creator euid = 0
external activation count = 0
security context = {
uid unset
asid = 100008
}
death port = 0x0
Useful commands:
-
This command installs/add a service:
-
launchctl load /Library/LaunchDaemons/com.ServiceExample.plist
-
-
This command enable a service to auto run after installed:
launchctl enable system/com.ServiceExample
-
This command disable a installed service from auto-running:
launchctl disable system/com.ServiceExample
-
This command start a service:
launchctl start system/com.ServiceExample
-
This command stop a service:
launchctl stop system/com.ServiceExample
-
This command remove a service:
-
launchctl unload /Library/LaunchDaemons/com.ServiceExample.plist
-
-
This command force execution of a recent loaded service:
launchctl kickstart -p /Library/LaunchDaemons/com.ServiceExample.plist
-
This command Stops and Disable a running service
launchctl bootout system/com.ServiceExample
You can change the above 'system domain' for a 'user domain' process.
Install/add means: include the service on the listing of services that the system knows about. (so you can start and stop it)
remove means: remove the service from the system listing of known services. (it will not be listed anymore if you execute
launchctl list
)
No files are deleted by removing the service, you just remove its .plist reference from the list of services. Once removed you cannot start/stop it. And it will not show on the launchctl list
. Unless you add it again with the load parameter.
You can do this by running the command:
launchctl list
In the first column of the output, you'll find the PID. If this is a number, the service is running - if it is listed as "-" it is not running.
Similarly you can check a single daemon like this:
launchctl list <daemon>
it will an output with various information - one of the lines looking like this:
"PID" = 310;
this means that the program is running, and the process ID is 310. If there's no "PID" line, then the daemon is not running.
There's no option to get an output identical to the one you know from systemctl for systemd. I'm not sure why you would want an identical output, or why you think there would be one. It wasn't possible for Apple to provide a compatible output when they released launchctl, as launchd predates systemd by 5 years. So in terms of "compatibility", it should really be the other way around - although I don't see the point in having compatible outputs there.
If you really want a similar output, it is relatively simple to create a small scripts that takes the output from launchctl and outputs it in a format similar to the output from systemctl. However it cannot be precisely the same, as the two utilities have diffent options and different information available to them.
For example the systemctl output usually contains stuff like log output (integrated into systemd, not integrated into launchd), CGroup information (Linux only feature) and pointers to documentation.
Similarly the launchctl output contains stuff like MachServices which is only available for macOS.
Please note that it is possible to setup launchd to start a daemon "on demand". This means that you specify for example that your web server software runs on TCP port 80 - and then launchd monitors that port and starts your daemon only when there's incoming requests. If you use launchctl you wouldn't find a PID for the daemon initially, but if you start sending requests to it - it will be started and have a PID. When requests stop, the daemon will be stopped again after a short while.