# Mac で再起動時にサービスを起動
- [Macの「ターミナル」でのlaunchdを使ったスクリプトの管理 - Apple サポート (日本)](https://support.apple.com/ja-jp/guide/terminal/apdc6c1077b-5d5d-4d35-9c19-60f2397b2369/mac)
`autossh` を起動する例
`autossh.sh`
```sh
#!/bin/bash
autossh -N -M 0 -R 8888:localhost:22 hoge
```
- [[autossh]]
- [[SSH Port Forwarding]]
`/Users/hibi/Library/LaunchAgents/local.hibi-mac.autossh.plist`
```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>local.hibi-mac.autossh</string>
<key>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/bin:/usr/bin:/usr/local/bin:/usr/sbin:/sbin:/opt/homebrew/bin</string>
</dict>
<key>ProgramArguments</key>
<array>
<string>/Users/hibi/autossh.sh</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>KeepAlive</key>
<true/>
<key>StandardOutPath</key>
<string>/Users/hibi/Downloads/standard_out.txt</string>
<key>StandardErrorPath</key>
<string>/Users/hibi/Downloads/standard_error.txt</string>
</dict>
</plist>
```
- `autossh` を brew で入れたので `PATH` に `/opt/homebrew/bin` を足している
- ネットワークが使えるようになるまでエラーで落ちないように `KeepAlive` を `true` にしてる
登録
```sh
launchctl load /Users/hibi/Library/LaunchAgents/local.hibi-mac.autossh.plist
```
確認
```sh
launchctl list | grep autossh
```
解除
```sh
launchctl unload /Users/hibi/Library/LaunchAgents/local.hibi-mac.autossh.plist
```