LoginSignup
10
15

More than 5 years have passed since last update.

毎日やる手作業コマンドをlaunchdを使って定期的に自動実行してみた

Posted at

はじめに

  • 骨休め的なTipsです。
  • Macの方対象となります。

背景

私がほぼパソコンを起動する日にやることとしては、
インストールしているソフトウェアのアップデートとゴミファイルの掃除です。

具体的には、以下のようなコマンドを実行しています。

Homebrewのアップデート

Homebrewとは

brew update && brew upgrade && brew cask upgrade

pipのアップデート

pipとpip-reviewとは

https://ja.wikipedia.org/wiki/Pip
https://github.com/jgonggrijp/pip-review

pip-review --auto

.DS_Storeの削除

.DS_Storeとは

find ~/. -name '.DS_Store' -type f -ls -delete

やること

今回はこれらをlaunchdというツールを利用して定期的に自動実行するように設定します。

自動実行するシェルスクリプトを作成する

シェルスクリプトの格納先ディレクトリの作成

mkdir ~/Scripts

アップデート系シェルスクリプトを作成する

  • 実行コマンドはフルパスで書いてください

anyenvとは

update.sh
/usr/local/bin/brew update && /usr/local/bin/brew upgrade && /usr/local/bin/brew cask upgrade
/Users/{ログインユーザー名}/.anyenv/envs/pyenv/shims/pip-review --auto

お掃除系シェルスクリプトを作成する

  • 実行コマンドはフルパスで書いてください
delete.sh
/usr/bin/find ~/. -name '.DS_Store' -type f -ls -delete

launchdにシェルスクリプトを実行するジョブを作成する

アップデート系のジョブ作成

~/Library/LaunchAgents/update.plist
<?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>update</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/zsh</string>
        <string>/Users/{ログインユーザー名}/Scripts/update.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>12</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    <key>StandardOutPath</key>
    <string>/Users/{ログインユーザー名}/Scripts/logs/update.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/{ログインユーザー名}/Scripts/logs/update_error.log</string>
</dict>
</plist>

解説

  • スクリプトの実行はZshを使うように指定してます。
Zshとは

<string>/usr/local/bin/zsh</string>
  • 実行時間は、」毎日お昼の12時に実行するように設定しています。
<dict>
    <key>Hour</key>
    <integer>12</integer>
    <key>Minute</key>
    <integer>0</integer>
</dict>
  • 実行ログとエラーログを以下のように出力するようにしております。
<key>StandardOutPath</key>
<string>/Users/{ログインユーザー名}/Scripts/logs/update.log</string>
<key>StandardErrorPath</key>
<string>/Users/{ログインユーザー名}/Scripts/logs/update_error.log</string>

お掃除系のジョブ作成

解説

  • update.plistとほぼ同様です。
  • こちらの実行時間は、毎日午前10時に実行するように設定しています。
~/Library/LaunchAgents/update.plist
<?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>delete</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/zsh</string>
        <string>/Users/{ログインユーザー名}/Scripts/delete.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>10</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
    <key>StandardOutPath</key>
    <string>/Users/{ログインユーザー名}/Scripts/logs/delete.log</string>
    <key>StandardErrorPath</key>
    <string>/Users/{ログインユーザー名}/Scripts/logs/delete_error.log</string>
</dict>
</plist>

launchdに作成したジョブを登録する

  • 上記で作成したジョブを以下のコマンドで登録すれば完了です。
launchctl load ~/Library/LaunchAgents/update.plist
launchctl load ~/Library/LaunchAgents/delete.plist

 さいごに

  • 実行されているかどうかは、上記で設定したログを見てみれば確認できるはずです。
  • ほかにも、好みによって定期的に実行したいものをどんどん登録していけるのでぜひやってみてください。
10
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
15