0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Mac スクリプトを作成とスケジューリングして自動実行

Last updated at Posted at 2025-07-16

概要

稼働時間外でもスクリプト処理を自動で実行できるようにする必要があり、今回設定の実施と手順化

手順

1.自動化したい処理をテキストエディタに記載して保存。今回はtestという名前で拡張子を.commandで保存。
→テキストエディタはVsCodeなどの好きなエディタで作成するか、nanoやVimで作成するのが良いかと思います。

#!/bin/bash
echo "自動実行テスト $(date)" >> ~/Desktop/test.log

2.自動実行したいスクリプトファイルに実行権限を付与

chmod +x ~/Desktop/test.command

3.スクリプトの自動実行設定
「システム設定またはシステム環境設定」→「プライバシーとセキュリティ」→「フルディスクアクセス内の左下の+ボタンを押下」

スクリーンショット 2025-07-16 10.16.37.png

4.cronのアクセス権限を付与
エクスプローラーを開いたら「command」+ 「shift」+ 「G」を押下→「/usr/sbin/cron」を入力して開く → フルディスクアクセス欄にcronが追加されていることを確認
→Macの再起動をするとより確実に反映されるかと思います。
スクリーンショット 2025-07-16 10.26.42.png
スクリーンショット 2025-07-16 10.28.15.png

5.crontabの設定編集コマンドを実行

crontab -e

6.テキスト内にスケジュール設定を追加。
スクリーンショット 2025-07-16 10.34.08.png
→下記がスケジュール設定の詳細

* * * * * <コマンド>
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └─ 曜日(0=日, 1=月, ..., 6=土)
│ │ │ └─── 月(1–12)
│ │ └───── 日(1–31)
│ └────── 時(0–23)
└──────── 分(0–59)

7.スケジュール設定を追加されたか確認

crontab -l

8.スケジュール設定されたスクリプトが実行されたことを確認
→下記実行結果

自動実行テスト Wed Jul 16 10:37:00 JST 2025

9.不要になったスケジュール設定削除。「crontab -e」を実行して記載したスケジュールを削除

実行コマンド

# スクリプト用ファイル作成
touch test.command

# 空ファイルを編集
vi test.command

# スクリプトファイルに実行権限を付与
chmod +x test.command

# crontabの編集
crontab -e

# crontabの情報を確認
crontab -l

参考

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?