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?

sleepwatcherを活用して.pdf自動圧縮システムの安定性を向上させる

Last updated at Posted at 2025-06-24

前提

※本記事は下記の補足です

上記システムの「監視・自動起動」機能を強化する為にsleepwatcherライブラリを導入し、Macのスリープ復帰時にも確実にプロセスが立ち上がる様にする方法を紹介します

何故sleepwatcherなのか?

macOSでは、スリープ・再起動・ログイン等をトリガーにしてプロセスを再起動させる方法が限られています

特にiCloud Driveとの同期を使う場合、スリープ明けに同期が遅延する事や、常駐デーモンが意図せず停止する事があります

sleepwatcherは、macのスリープ・ウェイク時に任意のシェルスクリプトを実行できる軽量なデーモンで、これによってmacが起きたタイミングで.pdf監視プロセスを明示的に再起動し、iCloudとプロセスの同期ミスを回避出来ます

導入手順

1. sleepwatcher のインストール

Homebrewで一発です

bash
brew install sleepwatcher

参考(公式):
https://formulae.brew.sh/formula/sleepwatcher

2. wakeupスクリプトの作成

~/.sleep や ~/.wakeup にスクリプトを記述する事で、スリープ前・復帰後に任意の処理を実行出来ます

今回は ~/.wakeup に以下の内容を記述:

.wakeup
#!/bin/bash

# ログ出力
echo "$(date): wakeup triggered" >> ~/Library/Mobile\ Documents/com~apple~CloudDocs/hiltlinc-sys/check_pdf/log/check_pdf.log

# iCloud上のディレクトリへ移動
cd ~/Library/Mobile\ Documents/com~apple~CloudDocs/hiltlinc-sys/check_pdf || exit

# Goプロジェクトのビルド&デーモン再起動
go mod tidy
go build -o process_pdf
launchctl unload ~/Library/LaunchAgents/com.user.check_pdf.plist
launchctl load ~/Library/LaunchAgents/com.user.check_pdf.plist >> log/check_pdf.log 2>&1

実行権限付与:

bash
chmod +x ~/.wakeup

3. sleepwatcher常駐化

sleepwatcherを起動する為のLaunchAgentを作成します

homebrew.mxcl.sleepwatcher.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>KeepAlive</key>
	<true/>
	<key>Label</key>
	<string>homebrew.mxcl.sleepwatcher</string>
	<key>LimitLoadToSessionType</key>
	<array>
		<string>Aqua</string>
		<string>Background</string>
		<string>LoginWindow</string>
		<string>StandardIO</string>
		<string>System</string>
	</array>
	<key>ProgramArguments</key>
	<array>
		<string>/opt/homebrew/opt/sleepwatcher/sbin/sleepwatcher</string>
		<string>-V</string>
		<string>-s</string>
		<string>/Users/{{YOUR_USERNAME}}/.sleep</string>
		<string>-w</string>
		<string>/Users/{{YOUR_USERNAME}}/.wakeup</string>
	</array>
	<key>RunAtLoad</key>
	<true/>
</dict>
</plist>

有効化:

bash
launchctl load ~/Library/LaunchAgents/de.bernhard-baehr.sleepwatcher.plist

iCloud環境での注意点

~/Library/Mobile Documents/ 以下はiCloud同期の対象のため、スリープ中に同期が中断する事があり、スリープ復帰後すぐにプロセスが再開されないと.pdf圧縮処理が漏れるリスクがあります

sleepwatcherにより復帰直後にGoプロジェクトをビルド&デーモン再起動する事で、確実に監視体制をリフレッシュ出来ます

まとめ

iCloud Driveを利用した.pdfの自動圧縮システムは便利ですが、macOSの省電力機能と同期のタイミングによって処理が漏れるケースが稀に存在します

今回の様にsleepwatcherを組み合わせる事で、より確実な常駐・再起動監視が可能になり、システムの安定性を向上させる事が出来ました

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?