# Launchd



#### Exécuter un script bash toutes les 5 minutes:

Copier le script en dehors du dossier Documents 

(sinon erreur 126: `/bin/bash: /Users/bruno/Documents/update-motd.sh: Operation not permitted`)

Créer une .plist:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>Label</key>
	<string>com.bruno21.update-motd</string>
	<key>ProgramArguments</key>
	<array>
		<string>sh</string>
		<string>-c</string>
		<string>${HOME}/.local/bin/update-motd.sh</string>
	</array>
	<key>StartCalendarInterval</key>
	<array>
		<dict>
			<key>Minute</key>
			<integer>0</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>5</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>10</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>15</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>20</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>25</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>30</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>35</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>40</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>45</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>50</integer>
		</dict>
		<dict>
			<key>Minute</key>
			<integer>55</integer>
		</dict>
	</array>
	<key>StandardOutPath</key>
	<string>/tmp/com.bruno21.update-motd.out</string>
	<key>StandardErrorPath</key>
	<string>/tmp/com.bruno21.update-motd.err</string>
</dict>
</plist>
```

La copier dans `$HOME/Library/LaunchAgent`

```bash
cp com.bruno21.update-motd.plist /Users/bruno/Library/LaunchAgents/
```

puis la charger:

```bash
launchctl load -w /Users/bruno/Library/LaunchAgents/com.bruno21.update-motd.plist
```

Voir si elle est activée:

```bash
launchctl list | grep .update-motd
-	0	com.bruno21.update-motd
```

Pour la retirer:

```bash
launchctl load -w /Users/bruno/Library/LaunchAgents/com.bruno21.update-motd.plist
```



```bash
cp update-motd.sh ${HOME}/.local/bin/
```

```bash
# test

~/.local/bin
➜ ./update-motd.sh
./update-motd.sh: line 46: /etc/motd: Permission denied

# si erreur: /Users/bruno/.local/bin/update-motd.sh: line 46: /etc/motd: Permission denied
sudo touch /etc/motd
sudo chmod 666 /etc/motd
```



https://apple.stackexchange.com/questions/392789/launchctl-plist-has-a-stderr-that-talks-about-how-getcwd-operation-not-permitted?newreg=7f5230c960a74d99b1105a9f66d9cfa2

https://rakhesh.com/mac/macos-launchctl-commands/

