🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: fj.unix
Article #1

macOS 起動時・終了時のスクリプト実行、launchctl で SIGTERM の捕捉

#1
From: Yasushi Shinjo
Date: Thu, 27 Aug 2020 09:39
25 lines
2364 bytes
新城@筑波大学情報です。こんにちは。

macOS で起動時にスクリプトを実行するのは、面倒になっています。昔は、
rc.local に書けば簡単でした。最近の技としては、crontab の @reboot を使う
ことです。それは良いのですが、shutdown 時にスクリプトを実行したくなりま
した。それで探したら、こんなのが出てきました。

    https://stackoverflow.com/questions/24200924/run-a-script-only-at-shutdown-not-log-off-or-restart-on-mac-os-x
    Run a script only at shutdown (not log off or restart) on Mac OS X
    asked Jun 13 '14 at 8:30,  answered Nov 24 '14 at 10:45 by freedev

ここで使われている技には関心しました。

- 起動時にシェルスクリプトを実後する。これは launchctl でできる。
- シェルスクリプトは終了しない。tail -f /dev/null & して wait $! でシグ
  ナルを待つ。
- SIGTERM が飛んできたら trap で終了の関数を実行する。

よく見ると、man 5 launchd.plist には signal のことも書いてありました。

macOS もこの辺(rc周り)、もう少し簡単にして欲しい。asl (Apple System
Logger) はつらい。asl には勝てる気がしません。

\\ 新城 靖 (しんじょう やすし) \\
\\ 筑波大学 情報工学        \\
Technical Details
Message-ID: <1ssgc8h1hh.fsf@is.tsukuba.ac.jp>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!apf1.newsdeef.eu!news.usenet.farm!..!..!not-for-mail

Show Full Headers
Complete Article Headers
Message-ID: <1ssgc8h1hh.fsf@is.tsukuba.ac.jp>
Subject: macOS 起動時・終了時のスクリプト実行、launchctl で SIGTERM の捕捉
Date: Thu, 27 Aug 2020 18:39:06 +0900
From: Yasushi Shinjo <yas@is.tsukuba.ac.jp>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!i2pn2.pugleaf.net!archive.newsdeef.eu!apf1.newsdeef.eu!news.usenet.farm!..!..!not-for-mail
Newsgroups: fj.sys.mac,fj.unix
Organization: Institute of Information Sciences & Electronics, University of Tsukuba, Japan
Lines: 25
Nntp-Posting-Host: top.cs.tsukuba.ac.jp
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-2022-jp
X-Trace: gama.is.tsukuba.ac.jp 1598518547 28678 130.158.81.193 (27 Aug 2020 08:55:47 GMT)
X-Complaints-To: news@gama.is.tsukuba.ac.jp
Nntp-Posting-Date: Thu, 27 Aug 2020 08:55:47 +0000 (UTC)
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (darwin)
Cancel-Lock: sha1:B7V959GYiz8xdyBSkdYDozudjD0=