Notepad/enter/Coding Tips (Classical)/Terminal Tips/System Client/OSX Apple Macbook/IDEs & APIs/BBEdit/Launchd.md

2.8 KiB

A MacOS specific version of a cron that automates scripts and programs.

For the man page for launchctl, please visit here.

In computing, launchd, a unified operating system service management framework, starts, stops and manages daemons, applications, processes, and scripts in macOS. It was introduced with Mac OS X Tiger and is licensed under the Apache License —Wikipedia. For more tips with launchd please head here.

The official source is found here. My version of LaunchControl for GUI troubleshooting is found here.


Here are some helpful tips;

View Services

sudo launchctl list

without Sudo

$ sudo launchctl list | grep ssh
-	0	com.openssh.sshd

$ launchctl list | grep ssh
3521	0	com.openssh.ssh-agent

Particular Service details

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

Stop and start service:

sudo launchctl stop com.openssh.sshd

sudo launchctl start com.openssh.sshd

Service definition via print:

sudo launchctl print system/com.openssh.sshd

Loading Services

# load
sudo launchctl load /System/Library/LaunchDaemons/ssh.plist

# enable
sudo launchctl enable system/com.openssh.sshd

# disable
sudo launchctl disable system/com.openssh.sshd 

# unload
sudo launchctl unload /System/Library/LaunchDaemons/ssh.plist

via Rakesh


More things to test with Launchd

  • Check that the script is an executable
```bash
chmod +x /Library/Scripts/com.apple.restart.sh
  • enable the full path to the script
```bash
#!/bin/bash
/sbin/shutdown -r now

Found by

which shutdown 

output: 

/sbin/shutdown

Ensure that the output is ACII file and not RTF or Pages or MS-Word

file /Library/Scripts/com.apple.restart.sh

output: 
/Library/Scripts/com.apple.restart.sh: Bourne-Again shell script text executable, ASCII text

Create a redirect for outputs:

<key>StandardOutPath</key>
<string>/tmp/com.apple.restart.stdout</string>
<key>StandardErrorPath</key>
<string>/tmp/com.apple.restart.stderr</string>

A great launchd blog post on here too.