0. はじめに
pmset と cron を使って bot の繰り返し設定をします。中身は Python の bot です。
1. pmset
基本コマンド
pmset -g sched
: 現在のスケジュール確認(繰り返し設定は一つしかできない)
sudo pmset repeat cancel
: 繰り返し設定の削除
man pmset
: マニュアルの表示
すでに 繰り返し設定があったので削除した。
設定
アーカイブボットのために電源オン設定をしました。
sudo pmset repeat wakeorpoweron MTWRF 11:59:00
のあとにパスワードを打ち込んだ。設定アプリのGUI 操作と同期しているらしい。繰り返し設定は1つでよいので これで終わりにした。
GPT による説明 "explain sudo pmset repeat wakeorpoweron MTWRF 11:59:00 "
sudo:
This is used to run the command with superuser (administrator) privileges. pmset requires elevated permissions because it modifies system-level settings.
pmset:
This is a command-line utility on macOS used to manage power management settings. It can control various aspects of how your Mac manages energy, including sleep, wake, and power schedules.
repeat:
This option is used to schedule a repeating event, meaning the action will occur regularly on specified days.
wakeorpoweron:
This is the event being scheduled. It tells the system to either wake from sleep or power on if it's currently shut down or in a power-off state.
MTWRF:
These letters represent the days of the week when the event should occur:
M: Monday
T: Tuesday
W: Wednesday
R: Thursday (R is commonly used as an abbreviation for Thursday to avoid confusion with Tuesday)
F: Friday
This means the event will be scheduled to occur from Monday to Friday.
11:59:00:
This is the time when the system will wake up or power on, in 24-hour format. In this case, it’s set for 11:59 AM.
Summary
When you run this command, your Mac will automatically wake up or power on at 11:59 AM every Monday, Tuesday, Wednesday, Thursday, and Friday. This can be useful for ensuring your system is ready for use at the same time each weekday, without needing to manually power it on or wake it up.
2. cron
cron は時間を表すギリシャ語から取られた 時間設定システム。裏で動いているデーモンと呼ばれるシステム。
cron は 電源が ON になっていないなどすると動かないので pmset で電源設定をした。
基本コマンド
export VISUAL=vim
: ターミナルで一時的に現状のプロセス(tab)において エディタを vim にする。
crontab -e
: cron 設定 (crontab ファイル) を編集する。
sudo crontab -e
: superuser 権限で cron 設定を編集する。
crontab -l
: 現状の cron 設定を見る。
.zshrc
に以下を書くと 時代遅れなエディタから Terminal のデフォルトエディタを VSCode に変更できる。
export VISUAL=~/code-wait.sh
export EDITOR="~/code-wait.sh"
EDITOR='code' crontab -e
: VSCode で cron 設定を編集できる。自分で設定をいじくっているとその通りいかないこともある。(EDITOR より VISUAL が優先されるなど)
注意
削除コマンドについて注意です。
crontab -r
: crontab ファイルを remove する。
e と r が QWERTY 配列で隣りにあるので間違わないように注意します。
crontab -e
をスニペットに登録しておくか、crontab -r
を使わないことを推奨します。
僕は EDITOR='code' crontab -e
を Snippety に登録しています。
はっきり言おう。不可逆な削除コマンドや終了ショートカットは押しにくいキー構成にするのが定石である。考えたやつは、ア・ホ。
実行
以下を実行します。ユーザは su ではなく普通の自分のユーザです。
su 権限で cron を動かしたい場合は VSCode だとエラーが出たので vim で安全第一に行うことを推奨します。
SHELL=/bin/zsh
0 12 * * mon-fri cd ~/arxiv_bot ; /bin/sh run.sh
arxiv_bot は使いたいフォルダ名です。ホームディレクトリ直下にあります。
run.sh
の中に Python コマンドの内容はすべて入っているので cron には書いていません。セミコロンは文の終了記号です。
実行プログラム の内容を変えたいときは
run.sh
のライブラリ内の中身を変えればいいだけなので cron 設定を書き換える必要はありません。頭が良いというのはこういう事をいいます
GPT が説明する (Explain '0 12 * * mon-fri cd ~/arxiv_bot ; /bin/sh run.sh' )
This part of the line specifies the schedule on which the command will run.
0:
This indicates the minute when the job will run. 0 means it will run at the 0th minute, i.e., at the beginning of the hour.
12:
This indicates the hour when the job will run. 12 means it will run at 12:00 PM (noon).
*:
The first * represents the day of the month. The * means any day of the month, so this job will run every day of the month that falls within the specified days of the week.
*:
The second * represents the month. The * means any month, so this job will run in every month of the year.
mon-fri:
This specifies the days of the week. mon-fri means the job will run from Monday to Friday.
- Command (cd ~/arxiv_bot ; /bin/sh run.sh)
This part specifies the command or script that will be executed at the specified time.
cd ~/arxiv_bot:
This command changes the current working directory to ~/arxiv_bot. The tilde (~) represents the home directory of the user who is running the cron job. This ensures that any subsequent commands are run in the context of the arxiv_bot directory.
;:
The semicolon is used to separate multiple commands on the same line. The second command will be executed only after the first command (cd ~/arxiv_bot) completes.
/bin/sh run.sh:
This command runs the run.sh script using the sh shell. The script run.sh is located in the arxiv_bot directory that was set by the cd command.
Summary
This cron job will:
Run at 12:00 PM (noon) every Monday to Friday.
Navigate to the ~/arxiv_bot directory.
Execute the run.sh script located in that directory using the /bin/sh shell.
This setup is typically used for automating tasks such as running scripts at specific times on weekdays.
ChatGPT の回答は必ずしも正しいとは限りません。重要な情報は確認するようにしてください。
3. 参考
vim の操作方法は こちら
こんなエディタはアナログしかなかった時代の計算尺みたいなもので、キーを覚える必要はないのでチートシートを参照して操作を正確にしていきましょう。
とあるエンジニアの備忘録: 3月 2019
http://kapibara3kapibara3.blogspot.com/2019/03/
この方は cron で pmset repeat 設定を書き換えています。 Python を実行した直後に次の Power ON を cron で予定させています。
← 書き換える手法を取らないと pmset schedule を日付とともに設定する必要があるので repeat 設定を書き換えたほうが簡単です。
cronでpmsetを複数登録する #Mac - Qiita
https://qiita.com/kaneyama/items/34f480a39dd0b7c46ec8
省エネルギー>>スケジュールでの自動起… - Apple コミュニティ https://discussionsjapan.apple.com/thread/10166562?sortBy=rank
Power Manager というアプリが紹介されている。
n. おわりに
cron と pmset を使って bot の繰り返し設定をしました。
sudo pmset repeat wakeorpoweron MTWRF 11:59:00
SHELL=/bin/zsh
0 12 * * mon-fri cd ~/arxiv_bot ; /bin/sh run.sh
Bot の前回記事はこちら