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