Notepad/Obsidian-Notepad/enter/Coding Tips (Classical)/Terminal Tips/Computers/Apple Macbook/Launchd.md

2.6 KiB

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

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.

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.