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

125 lines
2.6 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

A MacOS specific version of a [cron](obsidian://open?vault=enter&file=Coding%20Tips%20(Classical)%2FTerminal%20Tips%2FShells%2FComputers%20(operating%20system)%2FLinux%2FCrontab) 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](https://www.maketecheasier.com/use-launchd-run-scripts-on-schedule-macos/).
The official source is found [here](https://launchd.info/).
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](https://rakhesh.com/mac/macos-launchctl-commands/)
---
### 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. ](https://ellismin.com/2020/03/launchd-1/)